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#include <linux/module.h>
63#include <linux/moduleparam.h>
64#include <linux/kernel.h>
65#include <linux/types.h>
66#include <linux/errno.h>
67#include <linux/slab.h>
68#include <linux/usb.h>
69#include <linux/device.h>
70#include <linux/crc32.h>
71
72#include <asm/unaligned.h>
73#include <asm/byteorder.h>
74#include <asm/uaccess.h>
75
76#include <net/irda/irda.h>
77#include <net/irda/wrapper.h>
78#include <net/irda/crc.h>
79
80
81
82
83
84#define KING_VENDOR_ID 0x07c0
85#define KING_PRODUCT_ID 0x4200
86
87
88static struct usb_device_id dongles[] = {
89
90 { USB_DEVICE(KING_VENDOR_ID, KING_PRODUCT_ID) },
91 { }
92};
93
94MODULE_DEVICE_TABLE(usb, dongles);
95
96#define KINGSUN_MTT 0x07
97
98#define KINGSUN_FIFO_SIZE 4096
99#define KINGSUN_EP_IN 0
100#define KINGSUN_EP_OUT 1
101
102struct kingsun_cb {
103 struct usb_device *usbdev;
104 struct net_device *netdev;
105 struct irlap_cb *irlap;
106
107 struct qos_info qos;
108
109 __u8 *in_buf;
110 __u8 *out_buf;
111 __u8 max_rx;
112
113 __u8 max_tx;
114
115
116 iobuff_t rx_buff;
117 struct timeval rx_time;
118 spinlock_t lock;
119 int receiving;
120
121 __u8 ep_in;
122 __u8 ep_out;
123
124 struct urb *tx_urb;
125 struct urb *rx_urb;
126};
127
128
129static void kingsun_send_irq(struct urb *urb)
130{
131 struct kingsun_cb *kingsun = urb->context;
132 struct net_device *netdev = kingsun->netdev;
133
134
135 if (!netif_running(kingsun->netdev)) {
136 dev_err(&kingsun->usbdev->dev,
137 "kingsun_send_irq: Network not running!\n");
138 return;
139 }
140
141
142 if (urb->status != 0) {
143 dev_err(&kingsun->usbdev->dev,
144 "kingsun_send_irq: urb asynchronously failed - %d\n",
145 urb->status);
146 }
147 netif_wake_queue(netdev);
148}
149
150
151
152
153static netdev_tx_t kingsun_hard_xmit(struct sk_buff *skb,
154 struct net_device *netdev)
155{
156 struct kingsun_cb *kingsun;
157 int wraplen;
158 int ret = 0;
159
160 netif_stop_queue(netdev);
161
162
163 SKB_LINEAR_ASSERT(skb);
164
165 kingsun = netdev_priv(netdev);
166
167 spin_lock(&kingsun->lock);
168
169
170 wraplen = async_wrap_skb(skb,
171 kingsun->out_buf,
172 KINGSUN_FIFO_SIZE);
173
174
175 usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
176 usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
177 kingsun->out_buf, wraplen, kingsun_send_irq,
178 kingsun, 1);
179
180 if ((ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC))) {
181 dev_err(&kingsun->usbdev->dev,
182 "kingsun_hard_xmit: failed tx_urb submit: %d\n", ret);
183 switch (ret) {
184 case -ENODEV:
185 case -EPIPE:
186 break;
187 default:
188 netdev->stats.tx_errors++;
189 netif_start_queue(netdev);
190 }
191 } else {
192 netdev->stats.tx_packets++;
193 netdev->stats.tx_bytes += skb->len;
194 }
195
196 dev_kfree_skb(skb);
197 spin_unlock(&kingsun->lock);
198
199 return NETDEV_TX_OK;
200}
201
202
203static void kingsun_rcv_irq(struct urb *urb)
204{
205 struct kingsun_cb *kingsun = urb->context;
206 int ret;
207
208
209 if (!netif_running(kingsun->netdev)) {
210 kingsun->receiving = 0;
211 return;
212 }
213
214
215 if (urb->status != 0) {
216 dev_err(&kingsun->usbdev->dev,
217 "kingsun_rcv_irq: urb asynchronously failed - %d\n",
218 urb->status);
219 kingsun->receiving = 0;
220 return;
221 }
222
223 if (urb->actual_length == kingsun->max_rx) {
224 __u8 *bytes = urb->transfer_buffer;
225 int i;
226
227
228
229
230
231
232 if (bytes[0] >= 1 && bytes[0] < kingsun->max_rx) {
233 for (i = 1; i <= bytes[0]; i++) {
234 async_unwrap_char(kingsun->netdev,
235 &kingsun->netdev->stats,
236 &kingsun->rx_buff, bytes[i]);
237 }
238 do_gettimeofday(&kingsun->rx_time);
239 kingsun->receiving =
240 (kingsun->rx_buff.state != OUTSIDE_FRAME)
241 ? 1 : 0;
242 }
243 } else if (urb->actual_length > 0) {
244 dev_err(&kingsun->usbdev->dev,
245 "%s(): Unexpected response length, expected %d got %d\n",
246 __func__, kingsun->max_rx, urb->actual_length);
247 }
248
249 ret = usb_submit_urb(urb, GFP_ATOMIC);
250}
251
252
253
254
255
256
257static int kingsun_net_open(struct net_device *netdev)
258{
259 struct kingsun_cb *kingsun = netdev_priv(netdev);
260 int err = -ENOMEM;
261 char hwname[16];
262
263
264 kingsun->receiving = 0;
265
266
267 kingsun->rx_buff.in_frame = FALSE;
268 kingsun->rx_buff.state = OUTSIDE_FRAME;
269 kingsun->rx_buff.truesize = IRDA_SKB_MAX_MTU;
270 kingsun->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
271 if (!kingsun->rx_buff.skb)
272 goto free_mem;
273
274 skb_reserve(kingsun->rx_buff.skb, 1);
275 kingsun->rx_buff.head = kingsun->rx_buff.skb->data;
276 do_gettimeofday(&kingsun->rx_time);
277
278 kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
279 if (!kingsun->rx_urb)
280 goto free_mem;
281
282 kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
283 if (!kingsun->tx_urb)
284 goto free_mem;
285
286
287
288
289
290 sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
291 kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
292 if (!kingsun->irlap) {
293 dev_err(&kingsun->usbdev->dev, "irlap_open failed\n");
294 goto free_mem;
295 }
296
297
298 usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
299 usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
300 kingsun->in_buf, kingsun->max_rx,
301 kingsun_rcv_irq, kingsun, 1);
302 kingsun->rx_urb->status = 0;
303 err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
304 if (err) {
305 dev_err(&kingsun->usbdev->dev,
306 "first urb-submit failed: %d\n", err);
307 goto close_irlap;
308 }
309
310 netif_start_queue(netdev);
311
312
313
314
315
316
317
318
319
320
321 return 0;
322
323 close_irlap:
324 irlap_close(kingsun->irlap);
325 free_mem:
326 if (kingsun->tx_urb) {
327 usb_free_urb(kingsun->tx_urb);
328 kingsun->tx_urb = NULL;
329 }
330 if (kingsun->rx_urb) {
331 usb_free_urb(kingsun->rx_urb);
332 kingsun->rx_urb = NULL;
333 }
334 if (kingsun->rx_buff.skb) {
335 kfree_skb(kingsun->rx_buff.skb);
336 kingsun->rx_buff.skb = NULL;
337 kingsun->rx_buff.head = NULL;
338 }
339 return err;
340}
341
342
343
344
345
346
347
348static int kingsun_net_close(struct net_device *netdev)
349{
350 struct kingsun_cb *kingsun = netdev_priv(netdev);
351
352
353 netif_stop_queue(netdev);
354
355
356 usb_kill_urb(kingsun->tx_urb);
357 usb_kill_urb(kingsun->rx_urb);
358
359 usb_free_urb(kingsun->tx_urb);
360 usb_free_urb(kingsun->rx_urb);
361
362 kingsun->tx_urb = NULL;
363 kingsun->rx_urb = NULL;
364
365 kfree_skb(kingsun->rx_buff.skb);
366 kingsun->rx_buff.skb = NULL;
367 kingsun->rx_buff.head = NULL;
368 kingsun->rx_buff.in_frame = FALSE;
369 kingsun->rx_buff.state = OUTSIDE_FRAME;
370 kingsun->receiving = 0;
371
372
373 if (kingsun->irlap)
374 irlap_close(kingsun->irlap);
375
376 kingsun->irlap = NULL;
377
378 return 0;
379}
380
381
382
383
384static int kingsun_net_ioctl(struct net_device *netdev, struct ifreq *rq,
385 int cmd)
386{
387 struct if_irda_req *irq = (struct if_irda_req *) rq;
388 struct kingsun_cb *kingsun = netdev_priv(netdev);
389 int ret = 0;
390
391 switch (cmd) {
392 case SIOCSBANDWIDTH:
393 if (!capable(CAP_NET_ADMIN))
394 return -EPERM;
395
396
397 if (netif_device_present(kingsun->netdev))
398
399 ret = -EOPNOTSUPP;
400 break;
401
402 case SIOCSMEDIABUSY:
403 if (!capable(CAP_NET_ADMIN))
404 return -EPERM;
405
406
407 if (netif_running(kingsun->netdev))
408 irda_device_set_media_busy(kingsun->netdev, TRUE);
409 break;
410
411 case SIOCGRECEIVING:
412
413 irq->ifr_receiving = kingsun->receiving;
414 break;
415
416 default:
417 ret = -EOPNOTSUPP;
418 }
419
420 return ret;
421}
422
423static const struct net_device_ops kingsun_ops = {
424 .ndo_start_xmit = kingsun_hard_xmit,
425 .ndo_open = kingsun_net_open,
426 .ndo_stop = kingsun_net_close,
427 .ndo_do_ioctl = kingsun_net_ioctl,
428};
429
430
431
432
433
434
435static int kingsun_probe(struct usb_interface *intf,
436 const struct usb_device_id *id)
437{
438 struct usb_host_interface *interface;
439 struct usb_endpoint_descriptor *endpoint;
440
441 struct usb_device *dev = interface_to_usbdev(intf);
442 struct kingsun_cb *kingsun = NULL;
443 struct net_device *net = NULL;
444 int ret = -ENOMEM;
445 int pipe, maxp_in, maxp_out;
446 __u8 ep_in;
447 __u8 ep_out;
448
449
450
451
452 interface = intf->cur_altsetting;
453 if (interface->desc.bNumEndpoints != 2) {
454 dev_err(&intf->dev,
455 "kingsun-sir: expected 2 endpoints, found %d\n",
456 interface->desc.bNumEndpoints);
457 return -ENODEV;
458 }
459 endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
460 if (!usb_endpoint_is_int_in(endpoint)) {
461 dev_err(&intf->dev,
462 "kingsun-sir: endpoint 0 is not interrupt IN\n");
463 return -ENODEV;
464 }
465
466 ep_in = endpoint->bEndpointAddress;
467 pipe = usb_rcvintpipe(dev, ep_in);
468 maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
469 if (maxp_in > 255 || maxp_in <= 1) {
470 dev_err(&intf->dev,
471 "endpoint 0 has max packet size %d not in range\n",
472 maxp_in);
473 return -ENODEV;
474 }
475
476 endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
477 if (!usb_endpoint_is_int_out(endpoint)) {
478 dev_err(&intf->dev,
479 "kingsun-sir: endpoint 1 is not interrupt OUT\n");
480 return -ENODEV;
481 }
482
483 ep_out = endpoint->bEndpointAddress;
484 pipe = usb_sndintpipe(dev, ep_out);
485 maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
486
487
488 net = alloc_irdadev(sizeof(*kingsun));
489 if(!net)
490 goto err_out1;
491
492 SET_NETDEV_DEV(net, &intf->dev);
493 kingsun = netdev_priv(net);
494 kingsun->irlap = NULL;
495 kingsun->tx_urb = NULL;
496 kingsun->rx_urb = NULL;
497 kingsun->ep_in = ep_in;
498 kingsun->ep_out = ep_out;
499 kingsun->in_buf = NULL;
500 kingsun->out_buf = NULL;
501 kingsun->max_rx = (__u8)maxp_in;
502 kingsun->max_tx = (__u8)maxp_out;
503 kingsun->netdev = net;
504 kingsun->usbdev = dev;
505 kingsun->rx_buff.in_frame = FALSE;
506 kingsun->rx_buff.state = OUTSIDE_FRAME;
507 kingsun->rx_buff.skb = NULL;
508 kingsun->receiving = 0;
509 spin_lock_init(&kingsun->lock);
510
511
512 kingsun->in_buf = kmalloc(kingsun->max_rx, GFP_KERNEL);
513 if (!kingsun->in_buf)
514 goto free_mem;
515
516
517 kingsun->out_buf = kmalloc(KINGSUN_FIFO_SIZE, GFP_KERNEL);
518 if (!kingsun->out_buf)
519 goto free_mem;
520
521 printk(KERN_INFO "KingSun/DonShine IRDA/USB found at address %d, "
522 "Vendor: %x, Product: %x\n",
523 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
524 le16_to_cpu(dev->descriptor.idProduct));
525
526
527 irda_init_max_qos_capabilies(&kingsun->qos);
528
529
530 kingsun->qos.baud_rate.bits &= IR_9600;
531 kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
532 irda_qos_bits_to_value(&kingsun->qos);
533
534
535 net->netdev_ops = &kingsun_ops;
536
537 ret = register_netdev(net);
538 if (ret != 0)
539 goto free_mem;
540
541 dev_info(&net->dev, "IrDA: Registered KingSun/DonShine device %s\n",
542 net->name);
543
544 usb_set_intfdata(intf, kingsun);
545
546
547
548
549
550
551
552
553 return 0;
554
555free_mem:
556 kfree(kingsun->out_buf);
557 kfree(kingsun->in_buf);
558 free_netdev(net);
559err_out1:
560 return ret;
561}
562
563
564
565
566static void kingsun_disconnect(struct usb_interface *intf)
567{
568 struct kingsun_cb *kingsun = usb_get_intfdata(intf);
569
570 if (!kingsun)
571 return;
572
573 unregister_netdev(kingsun->netdev);
574
575
576 if (kingsun->tx_urb != NULL) {
577 usb_kill_urb(kingsun->tx_urb);
578 usb_free_urb(kingsun->tx_urb);
579 kingsun->tx_urb = NULL;
580 }
581 if (kingsun->rx_urb != NULL) {
582 usb_kill_urb(kingsun->rx_urb);
583 usb_free_urb(kingsun->rx_urb);
584 kingsun->rx_urb = NULL;
585 }
586
587 kfree(kingsun->out_buf);
588 kfree(kingsun->in_buf);
589 free_netdev(kingsun->netdev);
590
591 usb_set_intfdata(intf, NULL);
592}
593
594#ifdef CONFIG_PM
595
596static int kingsun_suspend(struct usb_interface *intf, pm_message_t message)
597{
598 struct kingsun_cb *kingsun = usb_get_intfdata(intf);
599
600 netif_device_detach(kingsun->netdev);
601 if (kingsun->tx_urb != NULL) usb_kill_urb(kingsun->tx_urb);
602 if (kingsun->rx_urb != NULL) usb_kill_urb(kingsun->rx_urb);
603 return 0;
604}
605
606
607static int kingsun_resume(struct usb_interface *intf)
608{
609 struct kingsun_cb *kingsun = usb_get_intfdata(intf);
610
611 if (kingsun->rx_urb != NULL)
612 usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
613 netif_device_attach(kingsun->netdev);
614
615 return 0;
616}
617#endif
618
619
620
621
622static struct usb_driver irda_driver = {
623 .name = "kingsun-sir",
624 .probe = kingsun_probe,
625 .disconnect = kingsun_disconnect,
626 .id_table = dongles,
627#ifdef CONFIG_PM
628 .suspend = kingsun_suspend,
629 .resume = kingsun_resume,
630#endif
631};
632
633module_usb_driver(irda_driver);
634
635MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
636MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun/DonShine");
637MODULE_LICENSE("GPL");
638