-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQMIDevice.c
executable file
·4227 lines (3548 loc) · 110 KB
/
QMIDevice.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
/*===========================================================================
FILE:
QMIDevice.c
DESCRIPTION:
Functions related to the QMI interface device
FUNCTIONS:
Generic functions
IsDeviceValid
PrintHex
GobiSetDownReason
GobiClearDownReason
GobiTestDownReason
Driver level asynchronous read functions
ResubmitIntURB
ReadCallback
IntCallback
StartRead
KillRead
Internal read/write functions
ReadAsync
UpSem
ReadSync
WriteSyncCallback
WriteSync
Internal memory management functions
GetClientID
ReleaseClientID
FindClientMem
AddToReadMemList
PopFromReadMemList
AddToNotifyList
NotifyAndPopNotifyList
AddToURBList
PopFromURBList
Internal userspace wrapper functions
UserspaceunlockedIOCTL
Userspace wrappers
UserspaceOpen
UserspaceIOCTL
UserspaceClose
UserspaceRead
UserspaceWrite
UserspacePoll
Initializer and destructor
RegisterQMIDevice
DeregisterQMIDevice
Driver level client management
QMIReady
QMIWDSCallback
SetupQMIWDSCallback
QMIDMSGetMEID
Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Code Aurora Forum nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
===========================================================================*/
//---------------------------------------------------------------------------
// Include Files
//---------------------------------------------------------------------------
#include <asm/unaligned.h>
#include "QMIDevice.h"
#include <linux/module.h>
//-----------------------------------------------------------------------------
// Definitions
//-----------------------------------------------------------------------------
extern int debug;
extern int interruptible;
#if (LINUX_VERSION_CODE <= KERNEL_VERSION( 2,6,22 ))
static int s_interval;
#endif
#if (LINUX_VERSION_CODE <= KERNEL_VERSION( 2,6,14 ))
#include <linux/devfs_fs_kernel.h>
static char devfs_name[32];
int device_create(struct class *class, struct device *parent, dev_t devt, const char *fmt, ...)
{
va_list vargs;
struct class_device *class_dev;
int err;
va_start(vargs, fmt);
vsnprintf(devfs_name, sizeof(devfs_name), fmt, vargs);
va_end(vargs);
class_dev = class_device_create(class, devt, parent, "%s", devfs_name);
if (IS_ERR(class_dev)) {
err = PTR_ERR(class_dev);
goto out;
}
err = devfs_mk_cdev(devt, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP, devfs_name);
if (err) {
class_device_destroy(class, devt);
goto out;
}
return 0;
out:
return err;
}
void device_destroy(struct class *class, dev_t devt)
{
class_device_destroy(class, devt);
devfs_remove(devfs_name);
}
#endif
#ifdef CONFIG_PM
// Prototype to GobiNetSuspend function
int GobiNetSuspend(
struct usb_interface * pIntf,
pm_message_t powerEvent );
#endif /* CONFIG_PM */
// IOCTL to generate a client ID for this service type
#define IOCTL_QMI_GET_SERVICE_FILE 0x8BE0 + 1
// IOCTL to get the VIDPID of the device
#define IOCTL_QMI_GET_DEVICE_VIDPID 0x8BE0 + 2
// IOCTL to get the MEID of the device
#define IOCTL_QMI_GET_DEVICE_MEID 0x8BE0 + 3
#define IOCTL_QMI_RELEASE_SERVICE_FILE_IOCTL (0x8BE0 + 4)
// CDC GET_ENCAPSULATED_RESPONSE packet
#define CDC_GET_ENCAPSULATED_RESPONSE_LE 0x01A1ll
#define CDC_GET_ENCAPSULATED_RESPONSE_BE 0xA101000000000000ll
/* The following masks filter the common part of the encapsulated response
* packet value for Gobi and QMI devices, ie. ignore usb interface number
*/
#define CDC_RSP_MASK_BE 0xFFFFFFFF00FFFFFFll
#define CDC_RSP_MASK_LE 0xFFFFFFE0FFFFFFFFll
const int i = 1;
#define is_bigendian() ( (*(char*)&i) == 0 )
#define CDC_GET_ENCAPSULATED_RESPONSE(pcdcrsp, pmask)\
{\
*pcdcrsp = is_bigendian() ? CDC_GET_ENCAPSULATED_RESPONSE_BE \
: CDC_GET_ENCAPSULATED_RESPONSE_LE ; \
*pmask = is_bigendian() ? CDC_RSP_MASK_BE \
: CDC_RSP_MASK_LE; \
}
// CDC CONNECTION_SPEED_CHANGE indication packet
#define CDC_CONNECTION_SPEED_CHANGE_LE 0x2AA1ll
#define CDC_CONNECTION_SPEED_CHANGE_BE 0xA12A000000000000ll
/* The following masks filter the common part of the connection speed change
* packet value for Gobi and QMI devices
*/
#define CDC_CONNSPD_MASK_BE 0xFFFFFFFFFFFF7FFFll
#define CDC_CONNSPD_MASK_LE 0XFFF7FFFFFFFFFFFFll
#define CDC_GET_CONNECTION_SPEED_CHANGE(pcdccscp, pmask)\
{\
*pcdccscp = is_bigendian() ? CDC_CONNECTION_SPEED_CHANGE_BE \
: CDC_CONNECTION_SPEED_CHANGE_LE ; \
*pmask = is_bigendian() ? CDC_CONNSPD_MASK_BE \
: CDC_CONNSPD_MASK_LE; \
}
#define SET_CONTROL_LINE_STATE_REQUEST_TYPE 0x21
#define SET_CONTROL_LINE_STATE_REQUEST 0x22
#define CONTROL_DTR 0x01
#define CONTROL_RTS 0x02
/*=========================================================================*/
// UserspaceQMIFops
// QMI device's userspace file operations
/*=========================================================================*/
struct file_operations UserspaceQMIFops =
{
.owner = THIS_MODULE,
.read = UserspaceRead,
.write = UserspaceWrite,
#ifdef CONFIG_COMPAT
.compat_ioctl = UserspaceunlockedIOCTL,
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION( 2,6,36 ))
.unlocked_ioctl = UserspaceunlockedIOCTL,
#else
.ioctl = UserspaceIOCTL,
#endif
.open = UserspaceOpen,
.flush = UserspaceClose,
.poll = UserspacePoll,
};
/*=========================================================================*/
// Generic functions
/*=========================================================================*/
u8 QMIXactionIDGet( sGobiUSBNet *pDev)
{
u8 transactionID;
if( 0 == (transactionID = atomic_add_return( 1, &pDev->mQMIDev.mQMICTLTransactionID)) )
{
transactionID = atomic_add_return( 1, &pDev->mQMIDev.mQMICTLTransactionID );
}
return transactionID;
}
static struct usb_endpoint_descriptor *GetEndpoint(
struct usb_interface *pintf,
int type,
int dir )
{
int i;
struct usb_host_interface *iface = pintf->cur_altsetting;
struct usb_endpoint_descriptor *pendp;
for( i = 0; i < iface->desc.bNumEndpoints; i++)
{
pendp = &iface->endpoint[i].desc;
if( ((pendp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir)
&&
(usb_endpoint_type(pendp) == type) )
{
return pendp;
}
}
return NULL;
}
/*===========================================================================
METHOD:
IsDeviceValid (Public Method)
DESCRIPTION:
Basic test to see if device memory is valid
PARAMETERS:
pDev [ I ] - Device specific memory
RETURN VALUE:
bool
===========================================================================*/
bool IsDeviceValid( sGobiUSBNet * pDev )
{
if (pDev == NULL)
{
return false;
}
if (pDev->mbQMIValid == false)
{
return false;
}
return true;
}
/*===========================================================================
METHOD:
PrintHex (Public Method)
DESCRIPTION:
Print Hex data, for debug purposes
PARAMETERS:
pBuffer [ I ] - Data buffer
bufSize [ I ] - Size of data buffer
RETURN VALUE:
None
===========================================================================*/
void PrintHex(
void * pBuffer,
u16 bufSize )
{
char * pPrintBuf;
u16 pos;
int status;
if (debug != 1)
{
return;
}
pPrintBuf = kmalloc( bufSize * 3 + 1, GFP_ATOMIC );
if (pPrintBuf == NULL)
{
DBG( "Unable to allocate buffer\n" );
return;
}
memset( pPrintBuf, 0 , bufSize * 3 + 1 );
for (pos = 0; pos < bufSize; pos++)
{
status = snprintf( (pPrintBuf + (pos * 3)),
4,
"%02X ",
*(u8 *)(pBuffer + pos) );
if (status != 3)
{
DBG( "snprintf error %d\n", status );
kfree( pPrintBuf );
return;
}
}
DBG( " : %s\n", pPrintBuf );
kfree( pPrintBuf );
pPrintBuf = NULL;
return;
}
/*===========================================================================
METHOD:
GobiSetDownReason (Public Method)
DESCRIPTION:
Sets mDownReason and turns carrier off
PARAMETERS
pDev [ I ] - Device specific memory
reason [ I ] - Reason device is down
RETURN VALUE:
None
===========================================================================*/
void GobiSetDownReason(
sGobiUSBNet * pDev,
u8 reason )
{
set_bit( reason, &pDev->mDownReason );
DBG("%s reason=%d, mDownReason=%x\n", __func__, reason, (unsigned)pDev->mDownReason);
netif_carrier_off( pDev->mpNetDev->net );
}
/*===========================================================================
METHOD:
GobiClearDownReason (Public Method)
DESCRIPTION:
Clear mDownReason and may turn carrier on
PARAMETERS
pDev [ I ] - Device specific memory
reason [ I ] - Reason device is no longer down
RETURN VALUE:
None
===========================================================================*/
void GobiClearDownReason(
sGobiUSBNet * pDev,
u8 reason )
{
clear_bit( reason, &pDev->mDownReason );
DBG("%s reason=%d, mDownReason=%x\n", __func__, reason, (unsigned)pDev->mDownReason);
#if 0 //(LINUX_VERSION_CODE >= KERNEL_VERSION( 3,11,0 ))
netif_carrier_on( pDev->mpNetDev->net );
#else
if (pDev->mDownReason == 0)
{
netif_carrier_on( pDev->mpNetDev->net );
}
#endif
}
/*===========================================================================
METHOD:
GobiTestDownReason (Public Method)
DESCRIPTION:
Test mDownReason and returns whether reason is set
PARAMETERS
pDev [ I ] - Device specific memory
reason [ I ] - Reason device is down
RETURN VALUE:
bool
===========================================================================*/
bool GobiTestDownReason(
sGobiUSBNet * pDev,
u8 reason )
{
return test_bit( reason, &pDev->mDownReason );
}
/*=========================================================================*/
// Driver level asynchronous read functions
/*=========================================================================*/
/*===========================================================================
METHOD:
ResubmitIntURB (Public Method)
DESCRIPTION:
Resubmit interrupt URB, re-using same values
PARAMETERS
pIntURB [ I ] - Interrupt URB
RETURN VALUE:
int - 0 for success
negative errno for failure
===========================================================================*/
int ResubmitIntURB( struct urb * pIntURB )
{
int status;
int interval;
// Sanity test
if ( (pIntURB == NULL)
|| (pIntURB->dev == NULL) )
{
return -EINVAL;
}
// Interval needs reset after every URB completion
#if (LINUX_VERSION_CODE > KERNEL_VERSION( 2,6,22 ))
interval = max((int)(pIntURB->ep->desc.bInterval),
(pIntURB->dev->speed == USB_SPEED_HIGH) ? 7 : 3);
#else
interval = s_interval;
#endif
// Reschedule interrupt URB
usb_fill_int_urb( pIntURB,
pIntURB->dev,
pIntURB->pipe,
pIntURB->transfer_buffer,
pIntURB->transfer_buffer_length,
pIntURB->complete,
pIntURB->context,
interval );
status = usb_submit_urb( pIntURB, GFP_ATOMIC );
if (status != 0)
{
DBG( "Error re-submitting Int URB %d\n", status );
}
return status;
}
/*===========================================================================
METHOD:
ReadCallback (Public Method)
DESCRIPTION:
Put the data in storage and notify anyone waiting for data
PARAMETERS
pReadURB [ I ] - URB this callback is run for
RETURN VALUE:
None
===========================================================================*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION( 2,6,14 ))
void ReadCallback( struct urb * pReadURB )
#else
void ReadCallback(struct urb *pReadURB, struct pt_regs *regs)
#endif
{
int result;
u16 clientID;
sClientMemList * pClientMem;
void * pData;
void * pDataCopy;
u16 dataSize;
sGobiUSBNet * pDev;
unsigned long flags;
u16 transactionID;
if (pReadURB == NULL)
{
DBG( "bad read URB\n" );
return;
}
pDev = pReadURB->context;
if (IsDeviceValid( pDev ) == false)
{
DBG( "Invalid device!\n" );
return;
}
if (pReadURB->status != 0)
{
DBG( "Read status = %d\n", pReadURB->status );
// Resubmit the interrupt URB
ResubmitIntURB( pDev->mQMIDev.mpIntURB );
return;
}
DBG( "Read %d bytes\n", pReadURB->actual_length );
pData = pReadURB->transfer_buffer;
dataSize = pReadURB->actual_length;
PrintHex( pData, dataSize );
result = ParseQMUX( &clientID,
pData,
dataSize );
if (result < 0)
{
DBG( "Read error parsing QMUX %d\n", result );
// Resubmit the interrupt URB
ResubmitIntURB( pDev->mQMIDev.mpIntURB );
return;
}
// Grab transaction ID
// Data large enough?
if (dataSize < result + 3)
{
DBG( "Data buffer too small to parse\n" );
// Resubmit the interrupt URB
ResubmitIntURB( pDev->mQMIDev.mpIntURB );
return;
}
// Transaction ID size is 1 for QMICTL, 2 for others
if (clientID == QMICTL)
{
transactionID = *(u8*)(pData + result + 1);
}
else
{
transactionID = le16_to_cpu( get_unaligned((u16*)(pData + result + 1)) );
}
// Critical section
spin_lock_irqsave( &pDev->mQMIDev.mClientMemLock, flags );
// Find memory storage for this service and Client ID
// Not using FindClientMem because it can't handle broadcasts
pClientMem = pDev->mQMIDev.mpClientMemList;
while (pClientMem != NULL)
{
if (pClientMem->mClientID == clientID
|| (pClientMem->mClientID | 0xff00) == clientID)
{
// Make copy of pData
pDataCopy = kmalloc( dataSize, GFP_ATOMIC );
if (pDataCopy == NULL)
{
DBG( "Error allocating client data memory\n" );
// End critical section
spin_unlock_irqrestore( &pDev->mQMIDev.mClientMemLock, flags );
// Resubmit the interrupt URB
ResubmitIntURB( pDev->mQMIDev.mpIntURB );
return;
}
memcpy( pDataCopy, pData, dataSize );
if (AddToReadMemList( pDev,
pClientMem->mClientID,
transactionID,
pDataCopy,
dataSize ) == false)
{
DBG( "Error allocating pReadMemListEntry "
"read will be discarded\n" );
kfree( pDataCopy );
// End critical section
spin_unlock_irqrestore( &pDev->mQMIDev.mClientMemLock, flags );
// Resubmit the interrupt URB
ResubmitIntURB( pDev->mQMIDev.mpIntURB );
return;
}
// Success
VDBG( "Creating new readListEntry for client 0x%04X, TID %x\n",
clientID,
transactionID );
// Notify this client data exists
NotifyAndPopNotifyList( pDev,
pClientMem->mClientID,
transactionID );
// Possibly notify poll() that data exists
wake_up_interruptible_sync( &pClientMem->mWaitQueue );
// Not a broadcast
if (clientID >> 8 != 0xff)
{
break;
}
}
// Next element
pClientMem = pClientMem->mpNext;
}
// End critical section
spin_unlock_irqrestore( &pDev->mQMIDev.mClientMemLock, flags );
// Resubmit the interrupt URB
ResubmitIntURB( pDev->mQMIDev.mpIntURB );
}
/*===========================================================================
METHOD:
IntCallback (Public Method)
DESCRIPTION:
Data is available, fire off a read URB
PARAMETERS
pIntURB [ I ] - URB this callback is run for
RETURN VALUE:
None
===========================================================================*/
#if (LINUX_VERSION_CODE > KERNEL_VERSION( 2,6,14 ))
void IntCallback( struct urb * pIntURB )
{
#else
void IntCallback(struct urb *pIntURB, struct pt_regs *regs)
{
#endif
int status;
u64 CDCEncResp;
u64 CDCEncRespMask;
sGobiUSBNet * pDev = (sGobiUSBNet *)pIntURB->context;
if (IsDeviceValid( pDev ) == false)
{
DBG( "Invalid device!\n" );
return;
}
// Verify this was a normal interrupt
if (pIntURB->status != 0)
{
DBG( "IntCallback: Int status = %d\n", pIntURB->status );
// Ignore EOVERFLOW errors
if (pIntURB->status != -EOVERFLOW)
{
// Read 'thread' dies here
return;
}
}
else
{
//TODO cast transfer_buffer to struct usb_cdc_notification
// CDC GET_ENCAPSULATED_RESPONSE
CDC_GET_ENCAPSULATED_RESPONSE(&CDCEncResp, &CDCEncRespMask)
VDBG( "IntCallback: Encapsulated Response = 0x%llx\n",
(*(u64*)pIntURB->transfer_buffer));
if ((pIntURB->actual_length == 8)
&& ((*(u64*)pIntURB->transfer_buffer & CDCEncRespMask) == CDCEncResp ) )
{
// Time to read
usb_fill_control_urb( pDev->mQMIDev.mpReadURB,
pDev->mpNetDev->udev,
usb_rcvctrlpipe( pDev->mpNetDev->udev, 0 ),
(unsigned char *)pDev->mQMIDev.mpReadSetupPacket,
pDev->mQMIDev.mpReadBuffer,
DEFAULT_READ_URB_LENGTH,
ReadCallback,
pDev );
status = usb_submit_urb( pDev->mQMIDev.mpReadURB, GFP_ATOMIC );
if (status != 0)
{
DBG( "Error submitting Read URB %d\n", status );
// Resubmit the interrupt urb
ResubmitIntURB( pIntURB );
return;
}
// Int URB will be resubmitted during ReadCallback
return;
}
// CDC CONNECTION_SPEED_CHANGE
else if ((pIntURB->actual_length == 16)
&& (CDC_GET_CONNECTION_SPEED_CHANGE(&CDCEncResp, &CDCEncRespMask))
&& ((*(u64*)pIntURB->transfer_buffer & CDCEncRespMask) == CDCEncResp ) )
{
DBG( "IntCallback: Connection Speed Change = 0x%llx\n",
(*(u64*)pIntURB->transfer_buffer));
// if upstream or downstream is 0, stop traffic. Otherwise resume it
if ((*(u32*)(pIntURB->transfer_buffer + 8) == 0)
|| (*(u32*)(pIntURB->transfer_buffer + 12) == 0))
{
GobiSetDownReason( pDev, CDC_CONNECTION_SPEED );
DBG( "traffic stopping due to CONNECTION_SPEED_CHANGE\n" );
}
else
{
GobiClearDownReason( pDev, CDC_CONNECTION_SPEED );
DBG( "resuming traffic due to CONNECTION_SPEED_CHANGE\n" );
}
}
else
{
DBG( "ignoring invalid interrupt in packet\n" );
PrintHex( pIntURB->transfer_buffer, pIntURB->actual_length );
}
}
// Resubmit the interrupt urb
ResubmitIntURB( pIntURB );
return;
}
/*===========================================================================
METHOD:
StartRead (Public Method)
DESCRIPTION:
Start continuous read "thread" (callback driven)
Note: In case of error, KillRead() should be run
to remove urbs and clean up memory.
PARAMETERS:
pDev [ I ] - Device specific memory
RETURN VALUE:
int - 0 for success
negative errno for failure
===========================================================================*/
int StartRead( sGobiUSBNet * pDev )
{
int interval;
struct usb_endpoint_descriptor *pendp;
if (IsDeviceValid( pDev ) == false)
{
DBG( "Invalid device!\n" );
return -ENXIO;
}
// Allocate URB buffers
pDev->mQMIDev.mpReadURB = usb_alloc_urb( 0, GFP_KERNEL );
if (pDev->mQMIDev.mpReadURB == NULL)
{
DBG( "Error allocating read urb\n" );
return -ENOMEM;
}
pDev->mQMIDev.mpIntURB = usb_alloc_urb( 0, GFP_KERNEL );
if (pDev->mQMIDev.mpIntURB == NULL)
{
DBG( "Error allocating int urb\n" );
usb_free_urb( pDev->mQMIDev.mpReadURB );
pDev->mQMIDev.mpReadURB = NULL;
return -ENOMEM;
}
// Create data buffers
pDev->mQMIDev.mpReadBuffer = kmalloc( DEFAULT_READ_URB_LENGTH, GFP_KERNEL );
if (pDev->mQMIDev.mpReadBuffer == NULL)
{
DBG( "Error allocating read buffer\n" );
usb_free_urb( pDev->mQMIDev.mpIntURB );
pDev->mQMIDev.mpIntURB = NULL;
usb_free_urb( pDev->mQMIDev.mpReadURB );
pDev->mQMIDev.mpReadURB = NULL;
return -ENOMEM;
}
pDev->mQMIDev.mpIntBuffer = kmalloc( 64, GFP_KERNEL );
if (pDev->mQMIDev.mpIntBuffer == NULL)
{
DBG( "Error allocating int buffer\n" );
kfree( pDev->mQMIDev.mpReadBuffer );
pDev->mQMIDev.mpReadBuffer = NULL;
usb_free_urb( pDev->mQMIDev.mpIntURB );
pDev->mQMIDev.mpIntURB = NULL;
usb_free_urb( pDev->mQMIDev.mpReadURB );
pDev->mQMIDev.mpReadURB = NULL;
return -ENOMEM;
}
pDev->mQMIDev.mpReadSetupPacket = kmalloc( sizeof( sURBSetupPacket ),
GFP_KERNEL );
if (pDev->mQMIDev.mpReadSetupPacket == NULL)
{
DBG( "Error allocating setup packet buffer\n" );
kfree( pDev->mQMIDev.mpIntBuffer );
pDev->mQMIDev.mpIntBuffer = NULL;
kfree( pDev->mQMIDev.mpReadBuffer );
pDev->mQMIDev.mpReadBuffer = NULL;
usb_free_urb( pDev->mQMIDev.mpIntURB );
pDev->mQMIDev.mpIntURB = NULL;
usb_free_urb( pDev->mQMIDev.mpReadURB );
pDev->mQMIDev.mpReadURB = NULL;
return -ENOMEM;
}
// CDC Get Encapsulated Response packet
pDev->mQMIDev.mpReadSetupPacket->mRequestType = 0xA1;
pDev->mQMIDev.mpReadSetupPacket->mRequestCode = 1;
pDev->mQMIDev.mpReadSetupPacket->mValue = 0;
pDev->mQMIDev.mpReadSetupPacket->mIndex =
cpu_to_le16(pDev->mpIntf->cur_altsetting->desc.bInterfaceNumber); /* interface number */
pDev->mQMIDev.mpReadSetupPacket->mLength = cpu_to_le16(DEFAULT_READ_URB_LENGTH);
pendp = GetEndpoint(pDev->mpIntf, USB_ENDPOINT_XFER_INT, USB_DIR_IN);
if (pendp == NULL)
{
DBG( "Invalid interrupt endpoint!\n" );
kfree(pDev->mQMIDev.mpReadSetupPacket);
pDev->mQMIDev.mpReadSetupPacket = NULL;
kfree( pDev->mQMIDev.mpIntBuffer );
pDev->mQMIDev.mpIntBuffer = NULL;
kfree( pDev->mQMIDev.mpReadBuffer );
pDev->mQMIDev.mpReadBuffer = NULL;
usb_free_urb( pDev->mQMIDev.mpIntURB );
pDev->mQMIDev.mpIntURB = NULL;
usb_free_urb( pDev->mQMIDev.mpReadURB );
pDev->mQMIDev.mpReadURB = NULL;
return -ENXIO;
}
// Interval needs reset after every URB completion
interval = max((int)(pendp->bInterval),
(pDev->mpNetDev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
#if (LINUX_VERSION_CODE <= KERNEL_VERSION( 2,6,22 ))
s_interval = interval;
#endif
// Schedule interrupt URB
usb_fill_int_urb( pDev->mQMIDev.mpIntURB,
pDev->mpNetDev->udev,
/* QMI interrupt endpoint for the following
* interface configuration: DM, NMEA, MDM, NET
*/
usb_rcvintpipe( pDev->mpNetDev->udev,
pendp->bEndpointAddress),
pDev->mQMIDev.mpIntBuffer,
min((int)le16_to_cpu(pendp->wMaxPacketSize), 64),
IntCallback,
pDev,
interval );
return usb_submit_urb( pDev->mQMIDev.mpIntURB, GFP_KERNEL );
}
/*===========================================================================
METHOD:
KillRead (Public Method)
DESCRIPTION:
Kill continuous read "thread"
PARAMETERS:
pDev [ I ] - Device specific memory
RETURN VALUE:
None
===========================================================================*/
void KillRead( sGobiUSBNet * pDev )
{
// Stop reading
if (pDev->mQMIDev.mpReadURB != NULL)
{
DBG( "Killng read URB\n" );
usb_kill_urb( pDev->mQMIDev.mpReadURB );
}
if (pDev->mQMIDev.mpIntURB != NULL)
{
DBG( "Killng int URB\n" );
usb_kill_urb( pDev->mQMIDev.mpIntURB );
}
// Release buffers
kfree( pDev->mQMIDev.mpReadSetupPacket );
pDev->mQMIDev.mpReadSetupPacket = NULL;
kfree( pDev->mQMIDev.mpReadBuffer );
pDev->mQMIDev.mpReadBuffer = NULL;
kfree( pDev->mQMIDev.mpIntBuffer );
pDev->mQMIDev.mpIntBuffer = NULL;
// Release URB's
usb_free_urb( pDev->mQMIDev.mpReadURB );
pDev->mQMIDev.mpReadURB = NULL;
usb_free_urb( pDev->mQMIDev.mpIntURB );
pDev->mQMIDev.mpIntURB = NULL;
}
/*=========================================================================*/
// Internal read/write functions
/*=========================================================================*/
/*===========================================================================
METHOD:
ReadAsync (Public Method)
DESCRIPTION:
Start asynchronous read
NOTE: Reading client's data store, not device
PARAMETERS:
pDev [ I ] - Device specific memory
clientID [ I ] - Requester's client ID
transactionID [ I ] - Transaction ID or 0 for any
pCallback [ I ] - Callback to be executed when data is available
pData [ I ] - Data buffer that willl be passed (unmodified)
to callback
RETURN VALUE:
int - 0 for success
negative errno for failure
===========================================================================*/
int ReadAsync(
sGobiUSBNet * pDev,
u16 clientID,
u16 transactionID,
void (*pCallback)(sGobiUSBNet*, u16, void *),
void * pData )
{
sClientMemList * pClientMem;
sReadMemList ** ppReadMemList;
unsigned long flags;
if (IsDeviceValid( pDev ) == false)
{
DBG( "Invalid device!\n" );
return -ENXIO;
}
// Critical section
spin_lock_irqsave( &pDev->mQMIDev.mClientMemLock, flags );
// Find memory storage for this client ID
pClientMem = FindClientMem( pDev, clientID );
if (pClientMem == NULL)
{
DBG( "Could not find matching client ID 0x%04X\n",
clientID );
// End critical section
spin_unlock_irqrestore( &pDev->mQMIDev.mClientMemLock, flags );
return -ENXIO;
}
ppReadMemList = &(pClientMem->mpList);