Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * |
| 3 | * Filename: kingsun-sir.c |
| 4 | * Version: 0.1.1 |
| 5 | * Description: Irda KingSun/DonShine USB Dongle |
| 6 | * Status: Experimental |
Samuel Ortiz | 66f5e51 | 2007-07-02 22:56:15 -0700 | [diff] [blame] | 7 | * Author: Alex Villacís Lasso <a_villacis@palosanto.com> |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 8 | * |
| 9 | * Based on stir4200 and mcs7780 drivers, with (strange?) differences |
| 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 | * This is my current (2007-04-25) understanding of how this dongle is supposed |
| 28 | * to work. This is based on reverse-engineering and examination of the packet |
| 29 | * data sent and received by the WinXP driver using USBSnoopy. Feel free to |
| 30 | * update here as more of this dongle is known: |
| 31 | * |
| 32 | * General: Unlike the other USB IrDA dongles, this particular dongle exposes, |
| 33 | * not two bulk (in and out) endpoints, but two *interrupt* ones. This dongle, |
| 34 | * like the bulk based ones (stir4200.c and mcs7780.c), requires polling in |
| 35 | * order to receive data. |
| 36 | * Transmission: Just like stir4200, this dongle uses a raw stream of data, |
| 37 | * which needs to be wrapped and escaped in a similar way as in stir4200.c. |
| 38 | * Reception: Poll-based, as in stir4200. Each read returns the contents of a |
| 39 | * 8-byte buffer, of which the first byte (LSB) indicates the number of bytes |
| 40 | * (1-7) of valid data contained within the remaining 7 bytes. For example, if |
| 41 | * the buffer had the following contents: |
| 42 | * 06 ff ff ff c0 01 04 aa |
| 43 | * This means that (06) there are 6 bytes of valid data. The byte 0xaa at the |
| 44 | * end is garbage (left over from a previous reception) and is discarded. |
| 45 | * If a read returns an "impossible" value as the length of valid data (such as |
| 46 | * 0x36) in the first byte, then the buffer is uninitialized (as is the case of |
| 47 | * first plug-in) and its contents should be discarded. There is currently no |
| 48 | * evidence that the top 5 bits of the 1st byte of the buffer can have values |
| 49 | * other than 0 once reception begins. |
| 50 | * Once valid bytes are collected, the assembled stream is a sequence of |
| 51 | * wrapped IrDA frames that is unwrapped and unescaped as in stir4200.c. |
| 52 | * BIG FAT WARNING: the dongle does *not* reset the RX buffer in any way after |
| 53 | * a successful read from the host, which means that in absence of further |
| 54 | * reception, repeated reads from the dongle will return the exact same |
| 55 | * contents repeatedly. Attempts to be smart and cache a previous read seem |
| 56 | * to result in corrupted packets, so this driver depends on the unwrap logic |
| 57 | * to sort out any repeated reads. |
| 58 | * Speed change: no commands observed so far to change speed, assumed fixed |
| 59 | * 9600bps (SIR). |
| 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> |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 69 | #include <linux/kref.h> |
| 70 | #include <linux/usb.h> |
| 71 | #include <linux/device.h> |
| 72 | #include <linux/crc32.h> |
| 73 | |
| 74 | #include <asm/unaligned.h> |
| 75 | #include <asm/byteorder.h> |
| 76 | #include <asm/uaccess.h> |
| 77 | |
| 78 | #include <net/irda/irda.h> |
| 79 | #include <net/irda/wrapper.h> |
| 80 | #include <net/irda/crc.h> |
| 81 | |
| 82 | /* |
| 83 | * According to lsusb, 0x07c0 is assigned to |
| 84 | * "Code Mercenaries Hard- und Software GmbH" |
| 85 | */ |
| 86 | #define KING_VENDOR_ID 0x07c0 |
| 87 | #define KING_PRODUCT_ID 0x4200 |
| 88 | |
| 89 | /* These are the currently known USB ids */ |
| 90 | static struct usb_device_id dongles[] = { |
| 91 | /* KingSun Co,Ltd IrDA/USB Bridge */ |
| 92 | { USB_DEVICE(KING_VENDOR_ID, KING_PRODUCT_ID) }, |
| 93 | { } |
| 94 | }; |
| 95 | |
| 96 | MODULE_DEVICE_TABLE(usb, dongles); |
| 97 | |
| 98 | #define KINGSUN_MTT 0x07 |
| 99 | |
| 100 | #define KINGSUN_FIFO_SIZE 4096 |
| 101 | #define KINGSUN_EP_IN 0 |
| 102 | #define KINGSUN_EP_OUT 1 |
| 103 | |
| 104 | struct kingsun_cb { |
| 105 | struct usb_device *usbdev; /* init: probe_irda */ |
| 106 | struct net_device *netdev; /* network layer */ |
| 107 | struct irlap_cb *irlap; /* The link layer we are binded to */ |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 108 | |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 109 | struct qos_info qos; |
| 110 | |
| 111 | __u8 *in_buf; /* receive buffer */ |
| 112 | __u8 *out_buf; /* transmit buffer */ |
| 113 | __u8 max_rx; /* max. atomic read from dongle |
| 114 | (usually 8), also size of in_buf */ |
| 115 | __u8 max_tx; /* max. atomic write to dongle |
| 116 | (usually 8) */ |
| 117 | |
| 118 | iobuff_t rx_buff; /* receive unwrap state machine */ |
| 119 | struct timeval rx_time; |
| 120 | spinlock_t lock; |
| 121 | int receiving; |
| 122 | |
| 123 | __u8 ep_in; |
| 124 | __u8 ep_out; |
| 125 | |
| 126 | struct urb *tx_urb; |
| 127 | struct urb *rx_urb; |
| 128 | }; |
| 129 | |
| 130 | /* Callback transmission routine */ |
| 131 | static void kingsun_send_irq(struct urb *urb) |
| 132 | { |
| 133 | struct kingsun_cb *kingsun = urb->context; |
| 134 | struct net_device *netdev = kingsun->netdev; |
| 135 | |
| 136 | /* in process of stopping, just drop data */ |
| 137 | if (!netif_running(kingsun->netdev)) { |
| 138 | err("kingsun_send_irq: Network not running!"); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | /* unlink, shutdown, unplug, other nasties */ |
| 143 | if (urb->status != 0) { |
| 144 | err("kingsun_send_irq: urb asynchronously failed - %d", |
| 145 | urb->status); |
| 146 | } |
| 147 | netif_wake_queue(netdev); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Called from net/core when new frame is available. |
| 152 | */ |
| 153 | static int kingsun_hard_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 154 | { |
| 155 | struct kingsun_cb *kingsun; |
| 156 | int wraplen; |
| 157 | int ret = 0; |
| 158 | |
| 159 | if (skb == NULL || netdev == NULL) |
| 160 | return -EINVAL; |
| 161 | |
| 162 | netif_stop_queue(netdev); |
| 163 | |
| 164 | /* the IRDA wrapping routines don't deal with non linear skb */ |
| 165 | SKB_LINEAR_ASSERT(skb); |
| 166 | |
| 167 | kingsun = netdev_priv(netdev); |
| 168 | |
| 169 | spin_lock(&kingsun->lock); |
| 170 | |
| 171 | /* Append data to the end of whatever data remains to be transmitted */ |
| 172 | wraplen = async_wrap_skb(skb, |
| 173 | kingsun->out_buf, |
| 174 | KINGSUN_FIFO_SIZE); |
| 175 | |
| 176 | /* Calculate how much data can be transmitted in this urb */ |
| 177 | usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev, |
| 178 | usb_sndintpipe(kingsun->usbdev, kingsun->ep_out), |
| 179 | kingsun->out_buf, wraplen, kingsun_send_irq, |
| 180 | kingsun, 1); |
| 181 | |
| 182 | if ((ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC))) { |
| 183 | err("kingsun_hard_xmit: failed tx_urb submit: %d", ret); |
| 184 | switch (ret) { |
| 185 | case -ENODEV: |
| 186 | case -EPIPE: |
| 187 | break; |
| 188 | default: |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 189 | netdev->stats.tx_errors++; |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 190 | netif_start_queue(netdev); |
| 191 | } |
| 192 | } else { |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 193 | netdev->stats.tx_packets++; |
| 194 | netdev->stats.tx_bytes += skb->len; |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | dev_kfree_skb(skb); |
| 198 | spin_unlock(&kingsun->lock); |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | /* Receive callback function */ |
| 204 | static void kingsun_rcv_irq(struct urb *urb) |
| 205 | { |
| 206 | struct kingsun_cb *kingsun = urb->context; |
| 207 | int ret; |
| 208 | |
| 209 | /* in process of stopping, just drop data */ |
| 210 | if (!netif_running(kingsun->netdev)) { |
| 211 | kingsun->receiving = 0; |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | /* unlink, shutdown, unplug, other nasties */ |
| 216 | if (urb->status != 0) { |
| 217 | err("kingsun_rcv_irq: urb asynchronously failed - %d", |
| 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 | /* The very first byte in the buffer indicates the length of |
| 228 | valid data in the read. This byte must be in the range |
| 229 | 1..kingsun->max_rx -1 . Values outside this range indicate |
| 230 | an uninitialized Rx buffer when the dongle has just been |
| 231 | plugged in. */ |
| 232 | if (bytes[0] >= 1 && bytes[0] < kingsun->max_rx) { |
| 233 | for (i = 1; i <= bytes[0]; i++) { |
| 234 | async_unwrap_char(kingsun->netdev, |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 235 | &kingsun->netdev->stats, |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 236 | &kingsun->rx_buff, bytes[i]); |
| 237 | } |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 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 | err("%s(): Unexpected response length, expected %d got %d", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 245 | __func__, kingsun->max_rx, urb->actual_length); |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 246 | } |
| 247 | /* This urb has already been filled in kingsun_net_open */ |
| 248 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Function kingsun_net_open (dev) |
| 253 | * |
| 254 | * Network device is taken up. Usually this is done by "ifconfig irda0 up" |
| 255 | */ |
| 256 | static int kingsun_net_open(struct net_device *netdev) |
| 257 | { |
| 258 | struct kingsun_cb *kingsun = netdev_priv(netdev); |
| 259 | int err = -ENOMEM; |
| 260 | char hwname[16]; |
| 261 | |
| 262 | /* At this point, urbs are NULL, and skb is NULL (see kingsun_probe) */ |
| 263 | kingsun->receiving = 0; |
| 264 | |
| 265 | /* Initialize for SIR to copy data directly into skb. */ |
| 266 | kingsun->rx_buff.in_frame = FALSE; |
| 267 | kingsun->rx_buff.state = OUTSIDE_FRAME; |
| 268 | kingsun->rx_buff.truesize = IRDA_SKB_MAX_MTU; |
| 269 | kingsun->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU); |
| 270 | if (!kingsun->rx_buff.skb) |
| 271 | goto free_mem; |
| 272 | |
| 273 | skb_reserve(kingsun->rx_buff.skb, 1); |
| 274 | kingsun->rx_buff.head = kingsun->rx_buff.skb->data; |
| 275 | do_gettimeofday(&kingsun->rx_time); |
| 276 | |
| 277 | kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 278 | if (!kingsun->rx_urb) |
| 279 | goto free_mem; |
| 280 | |
| 281 | kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 282 | if (!kingsun->tx_urb) |
| 283 | goto free_mem; |
| 284 | |
| 285 | /* |
| 286 | * Now that everything should be initialized properly, |
| 287 | * Open new IrLAP layer instance to take care of us... |
| 288 | */ |
| 289 | sprintf(hwname, "usb#%d", kingsun->usbdev->devnum); |
| 290 | kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname); |
| 291 | if (!kingsun->irlap) { |
| 292 | err("kingsun-sir: irlap_open failed"); |
| 293 | goto free_mem; |
| 294 | } |
| 295 | |
| 296 | /* Start first reception */ |
| 297 | usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev, |
| 298 | usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in), |
| 299 | kingsun->in_buf, kingsun->max_rx, |
| 300 | kingsun_rcv_irq, kingsun, 1); |
| 301 | kingsun->rx_urb->status = 0; |
| 302 | err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL); |
| 303 | if (err) { |
| 304 | err("kingsun-sir: first urb-submit failed: %d", err); |
| 305 | goto close_irlap; |
| 306 | } |
| 307 | |
| 308 | netif_start_queue(netdev); |
| 309 | |
| 310 | /* Situation at this point: |
| 311 | - all work buffers allocated |
| 312 | - urbs allocated and ready to fill |
| 313 | - max rx packet known (in max_rx) |
| 314 | - unwrap state machine initialized, in state outside of any frame |
| 315 | - receive request in progress |
| 316 | - IrLAP layer started, about to hand over packets to send |
| 317 | */ |
| 318 | |
| 319 | return 0; |
| 320 | |
| 321 | close_irlap: |
| 322 | irlap_close(kingsun->irlap); |
| 323 | free_mem: |
| 324 | if (kingsun->tx_urb) { |
| 325 | usb_free_urb(kingsun->tx_urb); |
| 326 | kingsun->tx_urb = NULL; |
| 327 | } |
| 328 | if (kingsun->rx_urb) { |
| 329 | usb_free_urb(kingsun->rx_urb); |
| 330 | kingsun->rx_urb = NULL; |
| 331 | } |
| 332 | if (kingsun->rx_buff.skb) { |
| 333 | kfree_skb(kingsun->rx_buff.skb); |
| 334 | kingsun->rx_buff.skb = NULL; |
| 335 | kingsun->rx_buff.head = NULL; |
| 336 | } |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Function kingsun_net_close (kingsun) |
| 342 | * |
| 343 | * Network device is taken down. Usually this is done by |
| 344 | * "ifconfig irda0 down" |
| 345 | */ |
| 346 | static int kingsun_net_close(struct net_device *netdev) |
| 347 | { |
| 348 | struct kingsun_cb *kingsun = netdev_priv(netdev); |
| 349 | |
| 350 | /* Stop transmit processing */ |
| 351 | netif_stop_queue(netdev); |
| 352 | |
| 353 | /* Mop up receive && transmit urb's */ |
| 354 | usb_kill_urb(kingsun->tx_urb); |
| 355 | usb_kill_urb(kingsun->rx_urb); |
| 356 | |
| 357 | usb_free_urb(kingsun->tx_urb); |
| 358 | usb_free_urb(kingsun->rx_urb); |
| 359 | |
| 360 | kingsun->tx_urb = NULL; |
| 361 | kingsun->rx_urb = NULL; |
| 362 | |
| 363 | kfree_skb(kingsun->rx_buff.skb); |
| 364 | kingsun->rx_buff.skb = NULL; |
| 365 | kingsun->rx_buff.head = NULL; |
| 366 | kingsun->rx_buff.in_frame = FALSE; |
| 367 | kingsun->rx_buff.state = OUTSIDE_FRAME; |
| 368 | kingsun->receiving = 0; |
| 369 | |
| 370 | /* Stop and remove instance of IrLAP */ |
| 371 | if (kingsun->irlap) |
| 372 | irlap_close(kingsun->irlap); |
| 373 | |
| 374 | kingsun->irlap = NULL; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * IOCTLs : Extra out-of-band network commands... |
| 381 | */ |
| 382 | static int kingsun_net_ioctl(struct net_device *netdev, struct ifreq *rq, |
| 383 | int cmd) |
| 384 | { |
| 385 | struct if_irda_req *irq = (struct if_irda_req *) rq; |
| 386 | struct kingsun_cb *kingsun = netdev_priv(netdev); |
| 387 | int ret = 0; |
| 388 | |
| 389 | switch (cmd) { |
| 390 | case SIOCSBANDWIDTH: /* Set bandwidth */ |
| 391 | if (!capable(CAP_NET_ADMIN)) |
| 392 | return -EPERM; |
| 393 | |
| 394 | /* Check if the device is still there */ |
| 395 | if (netif_device_present(kingsun->netdev)) |
| 396 | /* No observed commands for speed change */ |
| 397 | ret = -EOPNOTSUPP; |
| 398 | break; |
| 399 | |
| 400 | case SIOCSMEDIABUSY: /* Set media busy */ |
| 401 | if (!capable(CAP_NET_ADMIN)) |
| 402 | return -EPERM; |
| 403 | |
| 404 | /* Check if the IrDA stack is still there */ |
| 405 | if (netif_running(kingsun->netdev)) |
| 406 | irda_device_set_media_busy(kingsun->netdev, TRUE); |
| 407 | break; |
| 408 | |
| 409 | case SIOCGRECEIVING: |
| 410 | /* Only approximately true */ |
| 411 | irq->ifr_receiving = kingsun->receiving; |
| 412 | break; |
| 413 | |
| 414 | default: |
| 415 | ret = -EOPNOTSUPP; |
| 416 | } |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * This routine is called by the USB subsystem for each new device |
| 424 | * in the system. We need to check if the device is ours, and in |
| 425 | * this case start handling it. |
| 426 | */ |
| 427 | static int kingsun_probe(struct usb_interface *intf, |
| 428 | const struct usb_device_id *id) |
| 429 | { |
| 430 | struct usb_host_interface *interface; |
| 431 | struct usb_endpoint_descriptor *endpoint; |
| 432 | |
| 433 | struct usb_device *dev = interface_to_usbdev(intf); |
| 434 | struct kingsun_cb *kingsun = NULL; |
| 435 | struct net_device *net = NULL; |
| 436 | int ret = -ENOMEM; |
| 437 | int pipe, maxp_in, maxp_out; |
| 438 | __u8 ep_in; |
| 439 | __u8 ep_out; |
| 440 | |
| 441 | /* Check that there really are two interrupt endpoints. |
| 442 | Check based on the one in drivers/usb/input/usbmouse.c |
| 443 | */ |
| 444 | interface = intf->cur_altsetting; |
| 445 | if (interface->desc.bNumEndpoints != 2) { |
| 446 | err("kingsun-sir: expected 2 endpoints, found %d", |
| 447 | interface->desc.bNumEndpoints); |
| 448 | return -ENODEV; |
| 449 | } |
| 450 | endpoint = &interface->endpoint[KINGSUN_EP_IN].desc; |
| 451 | if (!usb_endpoint_is_int_in(endpoint)) { |
| 452 | err("kingsun-sir: endpoint 0 is not interrupt IN"); |
| 453 | return -ENODEV; |
| 454 | } |
| 455 | |
| 456 | ep_in = endpoint->bEndpointAddress; |
| 457 | pipe = usb_rcvintpipe(dev, ep_in); |
| 458 | maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); |
| 459 | if (maxp_in > 255 || maxp_in <= 1) { |
| 460 | err("%s: endpoint 0 has max packet size %d not in range", |
| 461 | __FILE__, maxp_in); |
| 462 | return -ENODEV; |
| 463 | } |
| 464 | |
| 465 | endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc; |
| 466 | if (!usb_endpoint_is_int_out(endpoint)) { |
| 467 | err("kingsun-sir: endpoint 1 is not interrupt OUT"); |
| 468 | return -ENODEV; |
| 469 | } |
| 470 | |
| 471 | ep_out = endpoint->bEndpointAddress; |
| 472 | pipe = usb_sndintpipe(dev, ep_out); |
| 473 | maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); |
| 474 | |
| 475 | /* Allocate network device container. */ |
| 476 | net = alloc_irdadev(sizeof(*kingsun)); |
| 477 | if(!net) |
| 478 | goto err_out1; |
| 479 | |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 480 | SET_NETDEV_DEV(net, &intf->dev); |
| 481 | kingsun = netdev_priv(net); |
| 482 | kingsun->irlap = NULL; |
| 483 | kingsun->tx_urb = NULL; |
| 484 | kingsun->rx_urb = NULL; |
| 485 | kingsun->ep_in = ep_in; |
| 486 | kingsun->ep_out = ep_out; |
| 487 | kingsun->in_buf = NULL; |
| 488 | kingsun->out_buf = NULL; |
| 489 | kingsun->max_rx = (__u8)maxp_in; |
| 490 | kingsun->max_tx = (__u8)maxp_out; |
| 491 | kingsun->netdev = net; |
| 492 | kingsun->usbdev = dev; |
| 493 | kingsun->rx_buff.in_frame = FALSE; |
| 494 | kingsun->rx_buff.state = OUTSIDE_FRAME; |
| 495 | kingsun->rx_buff.skb = NULL; |
| 496 | kingsun->receiving = 0; |
| 497 | spin_lock_init(&kingsun->lock); |
| 498 | |
| 499 | /* Allocate input buffer */ |
Jesper Juhl | 901ded2 | 2007-08-24 23:23:41 -0700 | [diff] [blame] | 500 | kingsun->in_buf = kmalloc(kingsun->max_rx, GFP_KERNEL); |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 501 | if (!kingsun->in_buf) |
| 502 | goto free_mem; |
| 503 | |
| 504 | /* Allocate output buffer */ |
Jesper Juhl | 901ded2 | 2007-08-24 23:23:41 -0700 | [diff] [blame] | 505 | kingsun->out_buf = kmalloc(KINGSUN_FIFO_SIZE, GFP_KERNEL); |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 506 | if (!kingsun->out_buf) |
| 507 | goto free_mem; |
| 508 | |
| 509 | printk(KERN_INFO "KingSun/DonShine IRDA/USB found at address %d, " |
| 510 | "Vendor: %x, Product: %x\n", |
| 511 | dev->devnum, le16_to_cpu(dev->descriptor.idVendor), |
| 512 | le16_to_cpu(dev->descriptor.idProduct)); |
| 513 | |
| 514 | /* Initialize QoS for this device */ |
| 515 | irda_init_max_qos_capabilies(&kingsun->qos); |
| 516 | |
| 517 | /* That's the Rx capability. */ |
| 518 | kingsun->qos.baud_rate.bits &= IR_9600; |
| 519 | kingsun->qos.min_turn_time.bits &= KINGSUN_MTT; |
| 520 | irda_qos_bits_to_value(&kingsun->qos); |
| 521 | |
| 522 | /* Override the network functions we need to use */ |
| 523 | net->hard_start_xmit = kingsun_hard_xmit; |
| 524 | net->open = kingsun_net_open; |
| 525 | net->stop = kingsun_net_close; |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 526 | net->do_ioctl = kingsun_net_ioctl; |
| 527 | |
| 528 | ret = register_netdev(net); |
| 529 | if (ret != 0) |
| 530 | goto free_mem; |
| 531 | |
Greg Kroah-Hartman | 880c9c6 | 2008-08-18 13:21:04 -0700 | [diff] [blame] | 532 | dev_info(&net->dev, "IrDA: Registered KingSun/DonShine device %s\n", |
| 533 | net->name); |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 534 | |
| 535 | usb_set_intfdata(intf, kingsun); |
| 536 | |
| 537 | /* Situation at this point: |
| 538 | - all work buffers allocated |
| 539 | - urbs not allocated, set to NULL |
| 540 | - max rx packet known (in max_rx) |
| 541 | - unwrap state machine (partially) initialized, but skb == NULL |
| 542 | */ |
| 543 | |
| 544 | return 0; |
| 545 | |
| 546 | free_mem: |
| 547 | if (kingsun->out_buf) kfree(kingsun->out_buf); |
| 548 | if (kingsun->in_buf) kfree(kingsun->in_buf); |
| 549 | free_netdev(net); |
| 550 | err_out1: |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * The current device is removed, the USB layer tell us to shut it down... |
| 556 | */ |
| 557 | static void kingsun_disconnect(struct usb_interface *intf) |
| 558 | { |
| 559 | struct kingsun_cb *kingsun = usb_get_intfdata(intf); |
| 560 | |
| 561 | if (!kingsun) |
| 562 | return; |
| 563 | |
| 564 | unregister_netdev(kingsun->netdev); |
| 565 | |
| 566 | /* Mop up receive && transmit urb's */ |
| 567 | if (kingsun->tx_urb != NULL) { |
| 568 | usb_kill_urb(kingsun->tx_urb); |
| 569 | usb_free_urb(kingsun->tx_urb); |
| 570 | kingsun->tx_urb = NULL; |
| 571 | } |
| 572 | if (kingsun->rx_urb != NULL) { |
| 573 | usb_kill_urb(kingsun->rx_urb); |
| 574 | usb_free_urb(kingsun->rx_urb); |
| 575 | kingsun->rx_urb = NULL; |
| 576 | } |
| 577 | |
| 578 | kfree(kingsun->out_buf); |
| 579 | kfree(kingsun->in_buf); |
| 580 | free_netdev(kingsun->netdev); |
| 581 | |
| 582 | usb_set_intfdata(intf, NULL); |
| 583 | } |
| 584 | |
| 585 | #ifdef CONFIG_PM |
| 586 | /* USB suspend, so power off the transmitter/receiver */ |
| 587 | static int kingsun_suspend(struct usb_interface *intf, pm_message_t message) |
| 588 | { |
| 589 | struct kingsun_cb *kingsun = usb_get_intfdata(intf); |
| 590 | |
| 591 | netif_device_detach(kingsun->netdev); |
| 592 | if (kingsun->tx_urb != NULL) usb_kill_urb(kingsun->tx_urb); |
| 593 | if (kingsun->rx_urb != NULL) usb_kill_urb(kingsun->rx_urb); |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | /* Coming out of suspend, so reset hardware */ |
| 598 | static int kingsun_resume(struct usb_interface *intf) |
| 599 | { |
| 600 | struct kingsun_cb *kingsun = usb_get_intfdata(intf); |
| 601 | |
| 602 | if (kingsun->rx_urb != NULL) |
| 603 | usb_submit_urb(kingsun->rx_urb, GFP_KERNEL); |
| 604 | netif_device_attach(kingsun->netdev); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | #endif |
| 609 | |
| 610 | /* |
| 611 | * USB device callbacks |
| 612 | */ |
| 613 | static struct usb_driver irda_driver = { |
| 614 | .name = "kingsun-sir", |
| 615 | .probe = kingsun_probe, |
| 616 | .disconnect = kingsun_disconnect, |
| 617 | .id_table = dongles, |
| 618 | #ifdef CONFIG_PM |
| 619 | .suspend = kingsun_suspend, |
| 620 | .resume = kingsun_resume, |
| 621 | #endif |
| 622 | }; |
| 623 | |
| 624 | /* |
| 625 | * Module insertion |
| 626 | */ |
| 627 | static int __init kingsun_init(void) |
| 628 | { |
| 629 | return usb_register(&irda_driver); |
| 630 | } |
| 631 | module_init(kingsun_init); |
| 632 | |
| 633 | /* |
| 634 | * Module removal |
| 635 | */ |
| 636 | static void __exit kingsun_cleanup(void) |
| 637 | { |
| 638 | /* Deregister the driver and remove all pending instances */ |
| 639 | usb_deregister(&irda_driver); |
| 640 | } |
| 641 | module_exit(kingsun_cleanup); |
| 642 | |
Samuel Ortiz | 66f5e51 | 2007-07-02 22:56:15 -0700 | [diff] [blame] | 643 | MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>"); |
Alex Villac�s Lasso | a2af421 | 2007-05-09 16:18:21 -0700 | [diff] [blame] | 644 | MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun/DonShine"); |
| 645 | MODULE_LICENSE("GPL"); |