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
|
/*
* AIRcable USB Bluetooth Dongle Driver.
*
* Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* The device works as an standard CDC device, it has 2 interfaces, the first
* one is for firmware access and the second is the serial one.
* The protocol is very simply, there are two posibilities reading or writing.
* When writting the first urb must have a Header that starts with 0x20 0x29 the
* next two bytes must say how much data will be sended.
* When reading the process is almost equal except that the header starts with
* 0x00 0x20.
*
* The device simply need some stuff to understand data comming from the usb
* buffer: The First and Second byte is used for a Header, the Third and Fourth
* tells the device the amount of information the package holds.
* Packages are 60 bytes long Header Stuff.
* When writting to the device the first two bytes of the header are 0x20 0x29
* When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
* situation, when too much data arrives to the device because it sends the data
* but with out the header. I will use a simply hack to override this situation,
* if there is data coming that does not contain any header, then that is data
* that must go directly to the tty, as there is no documentation about if there
* is any other control code, I will simply check for the first
* one.
*
* The driver registers himself with the USB-serial core and the USB Core. I had
* to implement a probe function agains USB-serial, because other way, the
* driver was attaching himself to both interfaces. I have tryed with different
* configurations of usb_serial_driver with out exit, only the probe function
* could handle this correctly.
*
* I have taken some info from a Greg Kroah-Hartman article:
* http://www.linuxjournal.com/article/6573
* And from Linux Device Driver Kit CD, which is a great work, the authors taken
* the work to recompile lots of information an knowladge in drivers development
* and made it all avaible inside a cd.
* URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
*
*/
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/circ_buf.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
static int debug;
/* Vendor and Product ID */
#define AIRCABLE_VID 0x16CA
#define AIRCABLE_USB_PID 0x1502
/* write buffer size defines */
#define AIRCABLE_BUF_SIZE 2048
/* Protocol Stuff */
#define HCI_HEADER_LENGTH 0x4
#define TX_HEADER_0 0x20
#define TX_HEADER_1 0x29
#define RX_HEADER_0 0x00
#define RX_HEADER_1 0x20
#define MAX_HCI_FRAMESIZE 60
#define HCI_COMPLETE_FRAME 64
/* rx_flags */
#define THROTTLED 0x01
#define ACTUALLY_THROTTLED 0x02
/*
* Version Information
*/
#define DRIVER_VERSION "v1.0b2"
#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>"
#define DRIVER_DESC "AIRcable USB Driver"
/* ID table that will be registered with USB core */
static struct usb_device_id id_table [] = {
{ USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
{ },
};
MODULE_DEVICE_TABLE(usb, id_table);
/* Internal Structure */
struct aircable_private {
spinlock_t rx_lock; /* spinlock for the receive lines */
struct circ_buf *tx_buf; /* write buffer */
struct circ_buf *rx_buf; /* read buffer */
int rx_flags; /* for throttilng */
struct work_struct rx_work; /* work cue for the receiving line */
struct usb_serial_port *port; /* USB port with which associated */
};
/* Private methods */
/* Circular Buffer Methods, code from ti_usb_3410_5052 used */
/*
* serial_buf_clear
*
* Clear out all data in the circular buffer.
*/
static void serial_buf_clear(struct circ_buf *cb)
{
cb->head = cb->tail = 0;
}
/*
* serial_buf_alloc
*
* Allocate a circular buffer and all associated memory.
*/
static struct circ_buf *serial_buf_alloc(void)
{
struct circ_buf *cb;
cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
if (cb == NULL)
return NULL;
cb->buf = kmalloc(AIRCABLE_BUF_SIZE, GFP_KERNEL);
if (cb->buf == NULL) {
kfree(cb);
return NULL;
}
serial_buf_clear(cb);
return cb;
}
/*
* serial_buf_free
*
* Free the buffer and all associated memory.
*/
static void serial_buf_free(struct circ_buf *cb)
{
kfree(cb->buf);
kfree(cb);
}
/*
* serial_buf_data_avail
*
* Return the number of bytes of data available in the circular
* buffer.
*/
static int serial_buf_data_avail(struct circ_buf *cb)
{
return CIRC_CNT(cb->head,cb->tail,AIRCABLE_BUF_SIZE);
}
/*
* serial_buf_put
*
* Copy data data from a user buffer and put it into the circular buffer.
* Restrict to the amount of space available.
*
* Return the number of bytes copied.
*/
static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
{
int c, ret = 0;
while (1) {
c = CIRC_SPACE_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
if (count < c)
c = count;
if (c <= 0)
break;
memcpy(cb->buf + cb->head, buf, c);
cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
buf += c;
count -= c;
ret= c;
}
return ret;
}
/*
* serial_buf_get
*
* Get data from the circular buffer and copy to the given buffer.
* Restrict to the amount of data available.
*
* Return the number of bytes copied.
*/
static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
{
int c, ret = 0;
while (1) {
c = CIRC_CNT_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
if (count < c)
c = count;
if (c <= 0)
break;
memcpy(buf, cb->buf + cb->tail, c);
cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
buf += c;
count -= c;
ret= c;
}
return ret;
}
/* End of circula buffer methods */
static void aircable_send(struct usb_serial_port *port)
{
int count, result;
struct aircable_private *priv = usb_get_serial_port_data(port);
unsigned char* buf;
dbg("%s - port %d", __FUNCTION__, port->number);
if (port->write_urb_busy)
return;
count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
if (count == 0)
return;
buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
if (!buf) {
err("%s- kzalloc(%d) failed.", __FUNCTION__,
count + HCI_HEADER_LENGTH);
return;
}
buf[0] = TX_HEADER_0;
buf[1] = TX_HEADER_1;
buf[2] = (unsigned char)count;
buf[3] = (unsigned char)(count >> 8);
serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
memcpy(port->write_urb->transfer_buffer, buf,
count + HCI_HEADER_LENGTH);
kfree(buf);
port->write_urb_busy = 1;
usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
count + HCI_HEADER_LENGTH,
port->write_urb->transfer_buffer);
port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
port->write_urb->dev = port->serial->dev;
result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
if (result) {
dev_err(&port->dev,
"%s - failed submitting write urb, error %d\n",
__FUNCTION__, result);
port->write_urb_busy = 0;
}
schedule_work(&port->work);
}
static void aircable_read(struct work_struct *work)
{
struct aircable_private *priv =
container_of(work, struct aircable_private, rx_work);
struct usb_serial_port *port = priv->port;
struct tty_struct *tty;
unsigned char *data;
int count;
if (priv->rx_flags & THROTTLED){
if (priv->rx_flags & ACTUALLY_THROTTLED<
|