6b13f685e
김민수
BSP 최초 추가
|
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
|
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/device.h>
#include <linux/crc32.h>
#include <asm/unaligned.h>
#include <asm/byteorder.h>
#include <asm/uaccess.h>
#include <net/irda/irda.h>
#include <net/irda/wrapper.h>
#include <net/irda/crc.h>
#define KING_VENDOR_ID 0x07c0
#define KING_PRODUCT_ID 0x4200
static struct usb_device_id dongles[] = {
{ USB_DEVICE(KING_VENDOR_ID, KING_PRODUCT_ID) },
{ }
};
MODULE_DEVICE_TABLE(usb, dongles);
#define KINGSUN_MTT 0x07
#define KINGSUN_FIFO_SIZE 4096
#define KINGSUN_EP_IN 0
#define KINGSUN_EP_OUT 1
struct kingsun_cb {
struct usb_device *usbdev;
struct net_device *netdev;
struct irlap_cb *irlap;
struct qos_info qos;
__u8 *in_buf;
__u8 *out_buf;
__u8 max_rx;
__u8 max_tx;
iobuff_t rx_buff;
struct timeval rx_time;
spinlock_t lock;
int receiving;
__u8 ep_in;
__u8 ep_out;
struct urb *tx_urb;
struct urb *rx_urb;
};
static void kingsun_send_irq(struct urb *urb)
{
struct kingsun_cb *kingsun = urb->context;
struct net_device *netdev = kingsun->netdev;
if (!netif_running(kingsun->netdev)) {
dev_err(&kingsun->usbdev->dev,
"kingsun_send_irq: Network not running!
");
return;
}
if (urb->status != 0) {
dev_err(&kingsun->usbdev->dev,
"kingsun_send_irq: urb asynchronously failed - %d
",
urb->status);
}
netif_wake_queue(netdev);
}
static netdev_tx_t kingsun_hard_xmit(struct sk_buff *skb,
struct net_device *netdev)
{
struct kingsun_cb *kingsun;
int wraplen;
int ret = 0;
netif_stop_queue(netdev);
SKB_LINEAR_ASSERT(skb);
kingsun = netdev_priv(netdev);
spin_lock(&kingsun->lock);
wraplen = async_wrap_skb(skb,
kingsun->out_buf,
KINGSUN_FIFO_SIZE);
usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
kingsun->out_buf, wraplen, kingsun_send_irq,
kingsun, 1);
if ((ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC))) {
dev_err(&kingsun->usbdev->dev,
"kingsun_hard_xmit: failed tx_urb submit: %d
", ret);
switch (ret) {
case -ENODEV:
case -EPIPE:
break;
default:
netdev->stats.tx_errors++;
netif_start_queue(netdev);
}
} else {
netdev->stats.tx_packets++;
netdev->stats.tx_bytes += skb->len;
}
dev_kfree_skb(skb);
spin_unlock(&kingsun->lock);
return NETDEV_TX_OK;
}
static void kingsun_rcv_irq(struct urb *urb)
{
struct kingsun_cb *kingsun = urb->context;
int ret;
if (!netif_running(kingsun->netdev)) {
kingsun->receiving = 0;
return;
}
if (urb->status != 0) {
dev_err(&kingsun->usbdev->dev,
"kingsun_rcv_irq: urb asynchronously failed - %d
",
urb->status);
kingsun->receiving = 0;
return;
}
if (urb->actual_length == kingsun->max_rx) {
__u8 *bytes = urb->transfer_buffer;
int i;
if (bytes[0] >= 1 && bytes[0] < kingsun->max_rx) {
for (i = 1; i <= bytes[0]; i++) {
async_unwrap_char(kingsun->netdev,
&kingsun->netdev->stats,
&kingsun->rx_buff, bytes[i]);
}
do_gettimeofday(&kingsun->rx_time);
kingsun->receiving =
(kingsun->rx_buff.state != OUTSIDE_FRAME)
? 1 : 0;
}
} else if (urb->actual_length > 0) {
dev_err(&kingsun->usbdev->dev,
"%s(): Unexpected response length, expected %d got %d
",
__func__, kingsun->max_rx, urb->actual_length);
}
ret = usb_submit_urb(urb, GFP_ATOMIC);
}
static int kingsun_net_open(struct net_device *netdev)
{
struct kingsun_cb *kingsun = netdev_priv(netdev);
int err = -ENOMEM;
char hwname[16];
kingsun->receiving = 0;
kingsun->rx_buff.in_frame = FALSE;
kingsun->rx_buff.state = OUTSIDE_FRAME;
kingsun->rx_buff.truesize = IRDA_SKB_MAX_MTU;
kingsun->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
if (!kingsun->rx_buff.skb)
goto free_mem;
skb_reserve(kingsun->rx_buff.skb, 1);
kingsun->rx_buff.head = kingsun->rx_buff.skb->data;
do_gettimeofday(&kingsun->rx_time);
kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!kingsun->rx_urb)
goto free_mem;
kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!kingsun->tx_urb)
goto free_mem;
sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
if (!kingsun->irlap) {
dev_err(&kingsun->usbdev->dev, "irlap_open failed
");
goto free_mem;
}
usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
kingsun->in_buf, kingsun->max_rx,
kingsun_rcv_irq, kingsun, 1);
kingsun->rx_urb->status = 0;
err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
if (err) {
dev_err(&kingsun->usbdev->dev,
"first urb-submit failed: %d
", err);
goto close_irlap;
}
netif_start_queue(netdev);
return 0;
close_irlap:
irlap_close(kingsun->irlap);
free_mem:
if (kingsun->tx_urb) {
usb_free_urb(kingsun->tx_urb);
kingsun->tx_urb = NULL;
}
if (kingsun->rx_urb) {
usb_free_urb(kingsun->rx_urb);
kingsun->rx_urb = NULL;
}
if (kingsun->rx_buff.skb) {
kfree_skb(kingsun->rx_buff.skb);
kingsun->rx_buff.skb = NULL;
kingsun->rx_buff.head = NULL;
}
return err;
}
static int kingsun_net_close(struct net_device *netdev)
{
struct kingsun_cb *kingsun = netdev_priv(netdev);
netif_stop_queue(netdev);
usb_kill_urb(kingsun->tx_urb);
usb_kill_urb(kingsun->rx_urb);
usb_free_urb(kingsun->tx_urb);
usb_free_urb(kingsun->rx_urb);
kingsun->tx_urb = NULL;
kingsun->rx_urb = NULL;
kfree_skb(kingsun->rx_buff.skb);
kingsun->rx_buff.skb = NULL;
kingsun->rx_buff.head = NULL;
kingsun->rx_buff.in_frame = FALSE;
kingsun->rx_buff.state = OUTSIDE_FRAME;
kingsun->receiving = 0;
if (kingsun->irlap)
irlap_close(kingsun->irlap);
kingsun->irlap = NULL;
return 0;
}
static int kingsun_net_ioctl(struct net_device *netdev, struct ifreq *rq,
int cmd)
{
struct if_irda_req *irq = (struct if_irda_req *) rq;
struct kingsun_cb *kingsun = netdev_priv(netdev);
int ret = 0;
switch (cmd) {
case SIOCSBANDWIDTH:
if (!capable(CAP_NET_ADMIN))
return -EPERM;
if (netif_device_present(kingsun->netdev))
ret = -EOPNOTSUPP;
break;
case SIOCSMEDIABUSY:
if (!capable(CAP_NET_ADMIN))
return -EPERM;
if (netif_running(kingsun->netdev))
irda_device_set_media_busy(kingsun->netdev, TRUE);
break;
case SIOCGRECEIVING:
irq->ifr_receiving = kingsun->receiving;
break;
default:
ret = -EOPNOTSUPP;
}
return ret;
}
static const struct net_device_ops kingsun_ops = {
.ndo_start_xmit = kingsun_hard_xmit,
.ndo_open = kingsun_net_open,
.ndo_stop = kingsun_net_close,
.ndo_do_ioctl = kingsun_net_ioctl,
};
static int kingsun_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_host_interface *interface;
struct usb_endpoint_descriptor *endpoint;
struct usb_device *dev = interface_to_usbdev(intf);
struct kingsun_cb *kingsun = NULL;
struct net_device *net = NULL;
int ret = -ENOMEM;
int pipe, maxp_in, maxp_out;
__u8 ep_in;
__u8 ep_out;
interface = intf->cur_altsetting;
if (interface->desc.bNumEndpoints != 2) {
dev_err(&intf->dev,
"kingsun-sir: expected 2 endpoints, found %d
",
interface->desc.bNumEndpoints);
return -ENODEV;
}
endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
if (!usb_endpoint_is_int_in(endpoint)) {
dev_err(&intf->dev,
"kingsun-sir: endpoint 0 is not interrupt IN
");
return -ENODEV;
}
ep_in = endpoint->bEndpointAddress;
pipe = usb_rcvintpipe(dev, ep_in);
maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
if (maxp_in > 255 || maxp_in <= 1) {
dev_err(&intf->dev,
"endpoint 0 has max packet size %d not in range
",
maxp_in);
return -ENODEV;
}
endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
if (!usb_endpoint_is_int_out(endpoint)) {
dev_err(&intf->dev,
"kingsun-sir: endpoint 1 is not interrupt OUT
");
return -ENODEV;
}
ep_out = endpoint->bEndpointAddress;
pipe = usb_sndintpipe(dev, ep_out);
maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
net = alloc_irdadev(sizeof(*kingsun));
if(!net)
goto err_out1;
SET_NETDEV_DEV(net, &intf->dev);
kingsun = netdev_priv(net);
kingsun->irlap = NULL;
kingsun->tx_urb = NULL;
kingsun->rx_urb = NULL;
kingsun->ep_in = ep_in;
kingsun->ep_out = ep_out;
kingsun->in_buf = NULL;
kingsun->out_buf = NULL;
kingsun->max_rx = (__u8)maxp_in;
kingsun->max_tx = (__u8)maxp_out;
kingsun->netdev = net;
kingsun->usbdev = dev;
kingsun->rx_buff.in_frame = FALSE;
kingsun->rx_buff.state = OUTSIDE_FRAME;
kingsun->rx_buff.skb = NULL;
kingsun->receiving = 0;
spin_lock_init(&kingsun->lock);
kingsun->in_buf = kmalloc(kingsun->max_rx, GFP_KERNEL);
if (!kingsun->in_buf)
goto free_mem;
kingsun->out_buf = kmalloc(KINGSUN_FIFO_SIZE, GFP_KERNEL);
if (!kingsun->out_buf)
goto free_mem;
printk(KERN_INFO "KingSun/DonShine IRDA/USB found at address %d, "
"Vendor: %x, Product: %x
",
dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
le16_to_cpu(dev->descriptor.idProduct));
irda_init_max_qos_capabilies(&kingsun->qos);
kingsun->qos.baud_rate.bits &= IR_9600;
kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
irda_qos_bits_to_value(&kingsun->qos);
net->netdev_ops = &kingsun_ops;
ret = register_netdev(net);
if (ret != 0)
goto free_mem;
dev_info(&net->dev, "IrDA: Registered KingSun/DonShine device %s
",
net->name);
usb_set_intfdata(intf, kingsun);
return 0;
free_mem:
if (kingsun->out_buf) kfree(kingsun->out_buf);
if (kingsun->in_buf) kfree(kingsun->in_buf);
free_netdev(net);
err_out1:
return ret;
}
static void kingsun_disconnect(struct usb_interface *intf)
{
struct kingsun_cb *kingsun = usb_get_intfdata(intf);
if (!kingsun)
return;
unregister_netdev(kingsun->netdev);
if (kingsun->tx_urb != NULL) {
usb_kill_urb(kingsun->tx_urb);
usb_free_urb(kingsun->tx_urb);
kingsun->tx_urb = NULL;
}
if (kingsun->rx_urb != NULL) {
usb_kill_urb(kingsun->rx_urb);
usb_free_urb(kingsun->rx_urb);
kingsun->rx_urb = NULL;
}
kfree(kingsun->out_buf);
kfree(kingsun->in_buf);
free_netdev(kingsun->netdev);
usb_set_intfdata(intf, NULL);
}
#ifdef CONFIG_PM
static int kingsun_suspend(struct usb_interface *intf, pm_message_t message)
{
struct kingsun_cb *kingsun = usb_get_intfdata(intf);
netif_device_detach(kingsun->netdev);
if (kingsun->tx_urb != NULL) usb_kill_urb(kingsun->tx_urb);
if (kingsun->rx_urb != NULL) usb_kill_urb(kingsun->rx_urb);
return 0;
}
static int kingsun_resume(struct usb_interface *intf)
{
struct kingsun_cb *kingsun = usb_get_intfdata(intf);
if (kingsun->rx_urb != NULL)
usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
netif_device_attach(kingsun->netdev);
return 0;
}
#endif
static struct usb_driver irda_driver = {
.name = "kingsun-sir",
.probe = kingsun_probe,
.disconnect = kingsun_disconnect,
.id_table = dongles,
#ifdef CONFIG_PM
.suspend = kingsun_suspend,
.resume = kingsun_resume,
#endif
};
module_usb_driver(irda_driver);
MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun/DonShine");
MODULE_LICENSE("GPL");
|