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