Merge branch 'devel' into next
[sfrench/cifs-2.6.git] / drivers / net / irda / ksdazzle-sir.c
1 /*****************************************************************************
2 *
3 * Filename:      ksdazzle.c
4 * Version:       0.1.2
5 * Description:   Irda KingSun Dazzle USB Dongle
6 * Status:        Experimental
7 * Author:        Alex Villacís Lasso <a_villacis@palosanto.com>
8 *
9 *    Based on stir4200, mcs7780, kingsun-sir drivers.
10 *
11 *    This program is free software; you can redistribute it and/or modify
12 *    it under the terms of the GNU General Public License as published by
13 *    the Free Software Foundation; either version 2 of the License.
14 *
15 *    This program is distributed in the hope that it will be useful,
16 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *    GNU General Public License for more details.
19 *
20 *    You should have received a copy of the GNU General Public License
21 *    along with this program; if not, write to the Free Software
22 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *****************************************************************************/
25
26 /*
27  * Following is my most current (2007-07-26) understanding of how the Kingsun
28  * 07D0:4100 dongle (sometimes known as the MA-660) is supposed to work. This
29  * information was deduced by examining the USB traffic captured with USBSnoopy
30  * from the WinXP driver. Feel free to update here as more of the dongle is
31  * known.
32  *
33  * General: This dongle exposes one interface with two interrupt endpoints, one
34  * IN and one OUT. In this regard, it is similar to what the Kingsun/Donshine
35  * dongle (07c0:4200) exposes. Traffic is raw and needs to be wrapped and
36  * unwrapped manually as in stir4200, kingsun-sir, and ks959-sir.
37  *
38  * Transmission: To transmit an IrDA frame, it is necessary to wrap it, then
39  * split it into multiple segments of up to 7 bytes each, and transmit each in
40  * sequence. It seems that sending a single big block (like kingsun-sir does)
41  * won't work with this dongle. Each segment needs to be prefixed with a value
42  * equal to (unsigned char)0xF8 + <number of bytes in segment>, inside a payload
43  * of exactly 8 bytes. For example, a segment of 1 byte gets prefixed by 0xF9,
44  * and one of 7 bytes gets prefixed by 0xFF. The bytes at the end of the
45  * payload, not considered by the prefix, are ignored (set to 0 by this
46  * implementation).
47  *
48  * Reception: To receive data, the driver must poll the dongle regularly (like
49  * kingsun-sir.c) with interrupt URBs. If data is available, it will be returned
50  * in payloads from 0 to 8 bytes long. When concatenated, these payloads form
51  * a raw IrDA stream that needs to be unwrapped as in stir4200 and kingsun-sir
52  *
53  * Speed change: To change the speed of the dongle, the driver prepares a
54  * control URB with the following as a setup packet:
55  *    bRequestType    USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
56  *    bRequest        0x09
57  *    wValue          0x0200
58  *    wIndex          0x0001
59  *    wLength         0x0008 (length of the payload)
60  * The payload is a 8-byte record, apparently identical to the one used in
61  * drivers/usb/serial/cypress_m8.c to change speed:
62  *     __u32 baudSpeed;
63  *    unsigned int dataBits : 2;    // 0 - 5 bits 3 - 8 bits
64  *    unsigned int : 1;
65  *    unsigned int stopBits : 1;
66  *    unsigned int parityEnable : 1;
67  *    unsigned int parityType : 1;
68  *    unsigned int : 1;
69  *    unsigned int reset : 1;
70  *    unsigned char reserved[3];    // set to 0
71  *
72  * For now only SIR speeds have been observed with this dongle. Therefore,
73  * nothing is known on what changes (if any) must be done to frame wrapping /
74  * unwrapping for higher than SIR speeds. This driver assumes no change is
75  * necessary and announces support for all the way to 115200 bps.
76  */
77
78 #include <linux/module.h>
79 #include <linux/moduleparam.h>
80 #include <linux/kernel.h>
81 #include <linux/types.h>
82 #include <linux/errno.h>
83 #include <linux/init.h>
84 #include <linux/slab.h>
85 #include <linux/kref.h>
86 #include <linux/usb.h>
87 #include <linux/device.h>
88 #include <linux/crc32.h>
89
90 #include <asm/unaligned.h>
91 #include <asm/byteorder.h>
92 #include <asm/uaccess.h>
93
94 #include <net/irda/irda.h>
95 #include <net/irda/wrapper.h>
96 #include <net/irda/crc.h>
97
98 #define KSDAZZLE_VENDOR_ID 0x07d0
99 #define KSDAZZLE_PRODUCT_ID 0x4100
100
101 /* These are the currently known USB ids */
102 static struct usb_device_id dongles[] = {
103         /* KingSun Co,Ltd  IrDA/USB Bridge */
104         {USB_DEVICE(KSDAZZLE_VENDOR_ID, KSDAZZLE_PRODUCT_ID)},
105         {}
106 };
107
108 MODULE_DEVICE_TABLE(usb, dongles);
109
110 #define KINGSUN_MTT 0x07
111 #define KINGSUN_REQ_RECV 0x01
112 #define KINGSUN_REQ_SEND 0x09
113
114 #define KINGSUN_SND_FIFO_SIZE    2048   /* Max packet we can send */
115 #define KINGSUN_RCV_MAX 2048    /* Max transfer we can receive */
116
117 struct ksdazzle_speedparams {
118         __le32 baudrate;        /* baud rate, little endian */
119         __u8 flags;
120         __u8 reserved[3];
121 } __attribute__ ((packed));
122
123 #define KS_DATA_5_BITS 0x00
124 #define KS_DATA_6_BITS 0x01
125 #define KS_DATA_7_BITS 0x02
126 #define KS_DATA_8_BITS 0x03
127
128 #define KS_STOP_BITS_1 0x00
129 #define KS_STOP_BITS_2 0x08
130
131 #define KS_PAR_DISABLE    0x00
132 #define KS_PAR_EVEN    0x10
133 #define KS_PAR_ODD    0x30
134 #define KS_RESET    0x80
135
136 #define KINGSUN_EP_IN                   0
137 #define KINGSUN_EP_OUT                  1
138
139 struct ksdazzle_cb {
140         struct usb_device *usbdev;      /* init: probe_irda */
141         struct net_device *netdev;      /* network layer */
142         struct irlap_cb *irlap; /* The link layer we are binded to */
143         struct net_device_stats stats;  /* network statistics */
144         struct qos_info qos;
145
146         struct urb *tx_urb;
147         __u8 *tx_buf_clear;
148         unsigned int tx_buf_clear_used;
149         unsigned int tx_buf_clear_sent;
150         __u8 tx_payload[8];
151
152         struct urb *rx_urb;
153         __u8 *rx_buf;
154         iobuff_t rx_unwrap_buff;
155
156         struct usb_ctrlrequest *speed_setuprequest;
157         struct urb *speed_urb;
158         struct ksdazzle_speedparams speedparams;
159         unsigned int new_speed;
160
161         __u8 ep_in;
162         __u8 ep_out;
163
164         spinlock_t lock;
165         int receiving;
166 };
167
168 /* Callback transmission routine */
169 static void ksdazzle_speed_irq(struct urb *urb)
170 {
171         /* unlink, shutdown, unplug, other nasties */
172         if (urb->status != 0) {
173                 err("ksdazzle_speed_irq: urb asynchronously failed - %d",
174                     urb->status);
175         }
176 }
177
178 /* Send a control request to change speed of the dongle */
179 static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
180 {
181         static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
182                 57600, 115200, 576000, 1152000, 4000000, 0
183         };
184         int err;
185         unsigned int i;
186
187         if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
188                 return -ENOMEM;
189
190         /* Check that requested speed is among the supported ones */
191         for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
192         if (supported_speeds[i] == 0)
193                 return -EOPNOTSUPP;
194
195         memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
196         kingsun->speedparams.baudrate = cpu_to_le32(speed);
197         kingsun->speedparams.flags = KS_DATA_8_BITS;
198
199         /* speed_setuprequest pre-filled in ksdazzle_probe */
200         usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
201                              usb_sndctrlpipe(kingsun->usbdev, 0),
202                              (unsigned char *)kingsun->speed_setuprequest,
203                              &(kingsun->speedparams),
204                              sizeof(struct ksdazzle_speedparams),
205                              ksdazzle_speed_irq, kingsun);
206         kingsun->speed_urb->status = 0;
207         err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
208
209         return err;
210 }
211
212 /* Submit one fragment of an IrDA frame to the dongle */
213 static void ksdazzle_send_irq(struct urb *urb);
214 static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
215 {
216         unsigned int wraplen;
217         int ret;
218
219         /* We can send at most 7 bytes of payload at a time */
220         wraplen = 7;
221         if (wraplen > kingsun->tx_buf_clear_used)
222                 wraplen = kingsun->tx_buf_clear_used;
223
224         /* Prepare payload prefix with used length */
225         memset(kingsun->tx_payload, 0, 8);
226         kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
227         memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
228
229         usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
230                          usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
231                          kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
232         kingsun->tx_urb->status = 0;
233         ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
234
235         /* Remember how much data was sent, in order to update at callback */
236         kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
237         return ret;
238 }
239
240 /* Callback transmission routine */
241 static void ksdazzle_send_irq(struct urb *urb)
242 {
243         struct ksdazzle_cb *kingsun = urb->context;
244         struct net_device *netdev = kingsun->netdev;
245         int ret = 0;
246
247         /* in process of stopping, just drop data */
248         if (!netif_running(kingsun->netdev)) {
249                 err("ksdazzle_send_irq: Network not running!");
250                 return;
251         }
252
253         /* unlink, shutdown, unplug, other nasties */
254         if (urb->status != 0) {
255                 err("ksdazzle_send_irq: urb asynchronously failed - %d",
256                     urb->status);
257                 return;
258         }
259
260         if (kingsun->tx_buf_clear_used > 0) {
261                 /* Update data remaining to be sent */
262                 if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
263                         memmove(kingsun->tx_buf_clear,
264                                 kingsun->tx_buf_clear +
265                                 kingsun->tx_buf_clear_sent,
266                                 kingsun->tx_buf_clear_used -
267                                 kingsun->tx_buf_clear_sent);
268                 }
269                 kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
270                 kingsun->tx_buf_clear_sent = 0;
271
272                 if (kingsun->tx_buf_clear_used > 0) {
273                         /* There is more data to be sent */
274                         if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
275                                 err("ksdazzle_send_irq: failed tx_urb submit: %d", ret);
276                                 switch (ret) {
277                                 case -ENODEV:
278                                 case -EPIPE:
279                                         break;
280                                 default:
281                                         kingsun->stats.tx_errors++;
282                                         netif_start_queue(netdev);
283                                 }
284                         }
285                 } else {
286                         /* All data sent, send next speed && wake network queue */
287                         if (kingsun->new_speed != -1 &&
288                             cpu_to_le32(kingsun->new_speed) !=
289                             kingsun->speedparams.baudrate)
290                                 ksdazzle_change_speed(kingsun,
291                                                       kingsun->new_speed);
292
293                         netif_wake_queue(netdev);
294                 }
295         }
296 }
297
298 /*
299  * Called from net/core when new frame is available.
300  */
301 static int ksdazzle_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
302 {
303         struct ksdazzle_cb *kingsun;
304         unsigned int wraplen;
305         int ret = 0;
306
307         if (skb == NULL || netdev == NULL)
308                 return -EINVAL;
309
310         netif_stop_queue(netdev);
311
312         /* the IRDA wrapping routines don't deal with non linear skb */
313         SKB_LINEAR_ASSERT(skb);
314
315         kingsun = netdev_priv(netdev);
316
317         spin_lock(&kingsun->lock);
318         kingsun->new_speed = irda_get_next_speed(skb);
319
320         /* Append data to the end of whatever data remains to be transmitted */
321         wraplen =
322             async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
323         kingsun->tx_buf_clear_used = wraplen;
324
325         if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
326                 err("ksdazzle_hard_xmit: failed tx_urb submit: %d", ret);
327                 switch (ret) {
328                 case -ENODEV:
329                 case -EPIPE:
330                         break;
331                 default:
332                         kingsun->stats.tx_errors++;
333                         netif_start_queue(netdev);
334                 }
335         } else {
336                 kingsun->stats.tx_packets++;
337                 kingsun->stats.tx_bytes += skb->len;
338
339         }
340
341         dev_kfree_skb(skb);
342         spin_unlock(&kingsun->lock);
343
344         return ret;
345 }
346
347 /* Receive callback function */
348 static void ksdazzle_rcv_irq(struct urb *urb)
349 {
350         struct ksdazzle_cb *kingsun = urb->context;
351
352         /* in process of stopping, just drop data */
353         if (!netif_running(kingsun->netdev)) {
354                 kingsun->receiving = 0;
355                 return;
356         }
357
358         /* unlink, shutdown, unplug, other nasties */
359         if (urb->status != 0) {
360                 err("ksdazzle_rcv_irq: urb asynchronously failed - %d",
361                     urb->status);
362                 kingsun->receiving = 0;
363                 return;
364         }
365
366         if (urb->actual_length > 0) {
367                 __u8 *bytes = urb->transfer_buffer;
368                 unsigned int i;
369
370                 for (i = 0; i < urb->actual_length; i++) {
371                         async_unwrap_char(kingsun->netdev, &kingsun->stats,
372                                           &kingsun->rx_unwrap_buff, bytes[i]);
373                 }
374                 kingsun->receiving =
375                     (kingsun->rx_unwrap_buff.state != OUTSIDE_FRAME) ? 1 : 0;
376         }
377
378         /* This urb has already been filled in ksdazzle_net_open. It is assumed that
379            urb keeps the pointer to the payload buffer.
380          */
381         urb->status = 0;
382         usb_submit_urb(urb, GFP_ATOMIC);
383 }
384
385 /*
386  * Function ksdazzle_net_open (dev)
387  *
388  *    Network device is taken up. Usually this is done by "ifconfig irda0 up"
389  */
390 static int ksdazzle_net_open(struct net_device *netdev)
391 {
392         struct ksdazzle_cb *kingsun = netdev_priv(netdev);
393         int err = -ENOMEM;
394         char hwname[16];
395
396         /* At this point, urbs are NULL, and skb is NULL (see ksdazzle_probe) */
397         kingsun->receiving = 0;
398
399         /* Initialize for SIR to copy data directly into skb.  */
400         kingsun->rx_unwrap_buff.in_frame = FALSE;
401         kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
402         kingsun->rx_unwrap_buff.truesize = IRDA_SKB_MAX_MTU;
403         kingsun->rx_unwrap_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
404         if (!kingsun->rx_unwrap_buff.skb)
405                 goto free_mem;
406
407         skb_reserve(kingsun->rx_unwrap_buff.skb, 1);
408         kingsun->rx_unwrap_buff.head = kingsun->rx_unwrap_buff.skb->data;
409
410         kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
411         if (!kingsun->rx_urb)
412                 goto free_mem;
413
414         kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
415         if (!kingsun->tx_urb)
416                 goto free_mem;
417
418         kingsun->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
419         if (!kingsun->speed_urb)
420                 goto free_mem;
421
422         /* Initialize speed for dongle */
423         kingsun->new_speed = 9600;
424         err = ksdazzle_change_speed(kingsun, 9600);
425         if (err < 0)
426                 goto free_mem;
427
428         /*
429          * Now that everything should be initialized properly,
430          * Open new IrLAP layer instance to take care of us...
431          */
432         sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
433         kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
434         if (!kingsun->irlap) {
435                 err("ksdazzle-sir: irlap_open failed");
436                 goto free_mem;
437         }
438
439         /* Start reception. */
440         usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
441                          usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
442                          kingsun->rx_buf, KINGSUN_RCV_MAX, ksdazzle_rcv_irq,
443                          kingsun, 1);
444         kingsun->rx_urb->status = 0;
445         err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
446         if (err) {
447                 err("ksdazzle-sir: first urb-submit failed: %d", err);
448                 goto close_irlap;
449         }
450
451         netif_start_queue(netdev);
452
453         /* Situation at this point:
454            - all work buffers allocated
455            - urbs allocated and ready to fill
456            - max rx packet known (in max_rx)
457            - unwrap state machine initialized, in state outside of any frame
458            - receive request in progress
459            - IrLAP layer started, about to hand over packets to send
460          */
461
462         return 0;
463
464       close_irlap:
465         irlap_close(kingsun->irlap);
466       free_mem:
467         usb_free_urb(kingsun->speed_urb);
468         kingsun->speed_urb = NULL;
469         usb_free_urb(kingsun->tx_urb);
470         kingsun->tx_urb = NULL;
471         usb_free_urb(kingsun->rx_urb);
472         kingsun->rx_urb = NULL;
473         if (kingsun->rx_unwrap_buff.skb) {
474                 kfree_skb(kingsun->rx_unwrap_buff.skb);
475                 kingsun->rx_unwrap_buff.skb = NULL;
476                 kingsun->rx_unwrap_buff.head = NULL;
477         }
478         return err;
479 }
480
481 /*
482  * Function ksdazzle_net_close (dev)
483  *
484  *    Network device is taken down. Usually this is done by
485  *    "ifconfig irda0 down"
486  */
487 static int ksdazzle_net_close(struct net_device *netdev)
488 {
489         struct ksdazzle_cb *kingsun = netdev_priv(netdev);
490
491         /* Stop transmit processing */
492         netif_stop_queue(netdev);
493
494         /* Mop up receive && transmit urb's */
495         usb_kill_urb(kingsun->tx_urb);
496         usb_free_urb(kingsun->tx_urb);
497         kingsun->tx_urb = NULL;
498
499         usb_kill_urb(kingsun->speed_urb);
500         usb_free_urb(kingsun->speed_urb);
501         kingsun->speed_urb = NULL;
502
503         usb_kill_urb(kingsun->rx_urb);
504         usb_free_urb(kingsun->rx_urb);
505         kingsun->rx_urb = NULL;
506
507         kfree_skb(kingsun->rx_unwrap_buff.skb);
508         kingsun->rx_unwrap_buff.skb = NULL;
509         kingsun->rx_unwrap_buff.head = NULL;
510         kingsun->rx_unwrap_buff.in_frame = FALSE;
511         kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
512         kingsun->receiving = 0;
513
514         /* Stop and remove instance of IrLAP */
515         irlap_close(kingsun->irlap);
516
517         kingsun->irlap = NULL;
518
519         return 0;
520 }
521
522 /*
523  * IOCTLs : Extra out-of-band network commands...
524  */
525 static int ksdazzle_net_ioctl(struct net_device *netdev, struct ifreq *rq,
526                               int cmd)
527 {
528         struct if_irda_req *irq = (struct if_irda_req *)rq;
529         struct ksdazzle_cb *kingsun = netdev_priv(netdev);
530         int ret = 0;
531
532         switch (cmd) {
533         case SIOCSBANDWIDTH:    /* Set bandwidth */
534                 if (!capable(CAP_NET_ADMIN))
535                         return -EPERM;
536
537                 /* Check if the device is still there */
538                 if (netif_device_present(kingsun->netdev))
539                         return ksdazzle_change_speed(kingsun,
540                                                      irq->ifr_baudrate);
541                 break;
542
543         case SIOCSMEDIABUSY:    /* Set media busy */
544                 if (!capable(CAP_NET_ADMIN))
545                         return -EPERM;
546
547                 /* Check if the IrDA stack is still there */
548                 if (netif_running(kingsun->netdev))
549                         irda_device_set_media_busy(kingsun->netdev, TRUE);
550                 break;
551
552         case SIOCGRECEIVING:
553                 /* Only approximately true */
554                 irq->ifr_receiving = kingsun->receiving;
555                 break;
556
557         default:
558                 ret = -EOPNOTSUPP;
559         }
560
561         return ret;
562 }
563
564 /*
565  * Get device stats (for /proc/net/dev and ifconfig)
566  */
567 static struct net_device_stats *ksdazzle_net_get_stats(struct net_device
568                                                        *netdev)
569 {
570         struct ksdazzle_cb *kingsun = netdev_priv(netdev);
571         return &kingsun->stats;
572 }
573
574 /*
575  * This routine is called by the USB subsystem for each new device
576  * in the system. We need to check if the device is ours, and in
577  * this case start handling it.
578  */
579 static int ksdazzle_probe(struct usb_interface *intf,
580                           const struct usb_device_id *id)
581 {
582         struct usb_host_interface *interface;
583         struct usb_endpoint_descriptor *endpoint;
584
585         struct usb_device *dev = interface_to_usbdev(intf);
586         struct ksdazzle_cb *kingsun = NULL;
587         struct net_device *net = NULL;
588         int ret = -ENOMEM;
589         int pipe, maxp_in, maxp_out;
590         __u8 ep_in;
591         __u8 ep_out;
592
593         /* Check that there really are two interrupt endpoints. Check based on the
594            one in drivers/usb/input/usbmouse.c
595          */
596         interface = intf->cur_altsetting;
597         if (interface->desc.bNumEndpoints != 2) {
598                 err("ksdazzle: expected 2 endpoints, found %d",
599                     interface->desc.bNumEndpoints);
600                 return -ENODEV;
601         }
602         endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
603         if (!usb_endpoint_is_int_in(endpoint)) {
604                 err("ksdazzle: endpoint 0 is not interrupt IN");
605                 return -ENODEV;
606         }
607
608         ep_in = endpoint->bEndpointAddress;
609         pipe = usb_rcvintpipe(dev, ep_in);
610         maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
611         if (maxp_in > 255 || maxp_in <= 1) {
612                 err("ksdazzle: endpoint 0 has max packet size %d not in range [2..255]", maxp_in);
613                 return -ENODEV;
614         }
615
616         endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
617         if (!usb_endpoint_is_int_out(endpoint)) {
618                 err("ksdazzle: endpoint 1 is not interrupt OUT");
619                 return -ENODEV;
620         }
621
622         ep_out = endpoint->bEndpointAddress;
623         pipe = usb_sndintpipe(dev, ep_out);
624         maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
625
626         /* Allocate network device container. */
627         net = alloc_irdadev(sizeof(*kingsun));
628         if (!net)
629                 goto err_out1;
630
631         SET_NETDEV_DEV(net, &intf->dev);
632         kingsun = netdev_priv(net);
633         kingsun->netdev = net;
634         kingsun->usbdev = dev;
635         kingsun->ep_in = ep_in;
636         kingsun->ep_out = ep_out;
637         kingsun->irlap = NULL;
638         kingsun->tx_urb = NULL;
639         kingsun->tx_buf_clear = NULL;
640         kingsun->tx_buf_clear_used = 0;
641         kingsun->tx_buf_clear_sent = 0;
642
643         kingsun->rx_urb = NULL;
644         kingsun->rx_buf = NULL;
645         kingsun->rx_unwrap_buff.in_frame = FALSE;
646         kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
647         kingsun->rx_unwrap_buff.skb = NULL;
648         kingsun->receiving = 0;
649         spin_lock_init(&kingsun->lock);
650
651         kingsun->speed_setuprequest = NULL;
652         kingsun->speed_urb = NULL;
653         kingsun->speedparams.baudrate = 0;
654
655         /* Allocate input buffer */
656         kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
657         if (!kingsun->rx_buf)
658                 goto free_mem;
659
660         /* Allocate output buffer */
661         kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
662         if (!kingsun->tx_buf_clear)
663                 goto free_mem;
664
665         /* Allocate and initialize speed setup packet */
666         kingsun->speed_setuprequest =
667             kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
668         if (!kingsun->speed_setuprequest)
669                 goto free_mem;
670         kingsun->speed_setuprequest->bRequestType =
671             USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
672         kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
673         kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
674         kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
675         kingsun->speed_setuprequest->wLength =
676             cpu_to_le16(sizeof(struct ksdazzle_speedparams));
677
678         printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
679                "Vendor: %x, Product: %x\n",
680                dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
681                le16_to_cpu(dev->descriptor.idProduct));
682
683         /* Initialize QoS for this device */
684         irda_init_max_qos_capabilies(&kingsun->qos);
685
686         /* Baud rates known to be supported. Please uncomment if devices (other
687            than a SonyEriccson K300 phone) can be shown to support higher speeds
688            with this dongle.
689          */
690         kingsun->qos.baud_rate.bits =
691             IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
692         kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
693         irda_qos_bits_to_value(&kingsun->qos);
694
695         /* Override the network functions we need to use */
696         net->hard_start_xmit = ksdazzle_hard_xmit;
697         net->open = ksdazzle_net_open;
698         net->stop = ksdazzle_net_close;
699         net->get_stats = ksdazzle_net_get_stats;
700         net->do_ioctl = ksdazzle_net_ioctl;
701
702         ret = register_netdev(net);
703         if (ret != 0)
704                 goto free_mem;
705
706         dev_info(&net->dev, "IrDA: Registered KingSun/Dazzle device %s\n",
707                  net->name);
708
709         usb_set_intfdata(intf, kingsun);
710
711         /* Situation at this point:
712            - all work buffers allocated
713            - setup requests pre-filled
714            - urbs not allocated, set to NULL
715            - max rx packet known (is KINGSUN_FIFO_SIZE)
716            - unwrap state machine (partially) initialized, but skb == NULL
717          */
718
719         return 0;
720
721       free_mem:
722         kfree(kingsun->speed_setuprequest);
723         kfree(kingsun->tx_buf_clear);
724         kfree(kingsun->rx_buf);
725         free_netdev(net);
726       err_out1:
727         return ret;
728 }
729
730 /*
731  * The current device is removed, the USB layer tell us to shut it down...
732  */
733 static void ksdazzle_disconnect(struct usb_interface *intf)
734 {
735         struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
736
737         if (!kingsun)
738                 return;
739
740         unregister_netdev(kingsun->netdev);
741
742         /* Mop up receive && transmit urb's */
743         usb_kill_urb(kingsun->speed_urb);
744         usb_free_urb(kingsun->speed_urb);
745         kingsun->speed_urb = NULL;
746
747         usb_kill_urb(kingsun->tx_urb);
748         usb_free_urb(kingsun->tx_urb);
749         kingsun->tx_urb = NULL;
750
751         usb_kill_urb(kingsun->rx_urb);
752         usb_free_urb(kingsun->rx_urb);
753         kingsun->rx_urb = NULL;
754
755         kfree(kingsun->speed_setuprequest);
756         kfree(kingsun->tx_buf_clear);
757         kfree(kingsun->rx_buf);
758         free_netdev(kingsun->netdev);
759
760         usb_set_intfdata(intf, NULL);
761 }
762
763 #ifdef CONFIG_PM
764 /* USB suspend, so power off the transmitter/receiver */
765 static int ksdazzle_suspend(struct usb_interface *intf, pm_message_t message)
766 {
767         struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
768
769         netif_device_detach(kingsun->netdev);
770         if (kingsun->speed_urb != NULL)
771                 usb_kill_urb(kingsun->speed_urb);
772         if (kingsun->tx_urb != NULL)
773                 usb_kill_urb(kingsun->tx_urb);
774         if (kingsun->rx_urb != NULL)
775                 usb_kill_urb(kingsun->rx_urb);
776         return 0;
777 }
778
779 /* Coming out of suspend, so reset hardware */
780 static int ksdazzle_resume(struct usb_interface *intf)
781 {
782         struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
783
784         if (kingsun->rx_urb != NULL) {
785                 /* Setup request already filled in ksdazzle_probe */
786                 usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
787         }
788         netif_device_attach(kingsun->netdev);
789
790         return 0;
791 }
792 #endif
793
794 /*
795  * USB device callbacks
796  */
797 static struct usb_driver irda_driver = {
798         .name = "ksdazzle-sir",
799         .probe = ksdazzle_probe,
800         .disconnect = ksdazzle_disconnect,
801         .id_table = dongles,
802 #ifdef CONFIG_PM
803         .suspend = ksdazzle_suspend,
804         .resume = ksdazzle_resume,
805 #endif
806 };
807
808 /*
809  * Module insertion
810  */
811 static int __init ksdazzle_init(void)
812 {
813         return usb_register(&irda_driver);
814 }
815
816 module_init(ksdazzle_init);
817
818 /*
819  * Module removal
820  */
821 static void __exit ksdazzle_cleanup(void)
822 {
823         /* Deregister the driver and remove all pending instances */
824         usb_deregister(&irda_driver);
825 }
826
827 module_exit(ksdazzle_cleanup);
828
829 MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
830 MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun Dazzle");
831 MODULE_LICENSE("GPL");