[IrDA]: MCS7780 usb_driver struct should be static
[linux-2.6/pdupreez.git] / drivers / net / irda / mcs7780.c
blob47f6f64d604c980b2c5a553a5500a01fca4b1b78
1 /*****************************************************************************
3 * Filename: mcs7780.c
4 * Version: 0.4-alpha
5 * Description: Irda MosChip USB Dongle Driver
6 * Authors: Lukasz Stelmach <stlman@poczta.fm>
7 * Brian Pugh <bpugh@cs.pdx.edu>
8 * Judy Fischbach <jfisch@cs.pdx.edu>
10 * Based on stir4200 driver, but some things done differently.
11 * Based on earlier driver by Paul Stewart <stewart@parc.com>
13 * Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
14 * Copyright (C) 2001, Dag Brattli <dag@brattli.net>
15 * Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
16 * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
17 * Copyright (C) 2005, Lukasz Stelmach <stlman@poczta.fm>
18 * Copyright (C) 2005, Brian Pugh <bpugh@cs.pdx.edu>
19 * Copyright (C) 2005, Judy Fischbach <jfisch@cs.pdx.edu>
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *****************************************************************************/
38 * MCS7780 is a simple USB to IrDA bridge by MosChip. It is neither
39 * compatibile with irda-usb nor with stir4200. Although it is quite
40 * similar to the later as far as general idea of operation is concerned.
41 * That is it requires the software to do all the framing job at SIR speeds.
42 * The hardware does take care of the framing at MIR and FIR speeds.
43 * It supports all speeds from 2400 through 4Mbps
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/config.h>
49 #include <linux/kernel.h>
50 #include <linux/types.h>
51 #include <linux/errno.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/module.h>
55 #include <linux/kref.h>
56 #include <linux/usb.h>
57 #include <linux/device.h>
58 #include <linux/crc32.h>
60 #include <asm/unaligned.h>
61 #include <asm/byteorder.h>
62 #include <asm/uaccess.h>
64 #include <net/irda/irda.h>
65 #include <net/irda/wrapper.h>
66 #include <net/irda/crc.h>
68 #include "mcs7780.h"
70 #define MCS_VENDOR_ID 0x9710
71 #define MCS_PRODUCT_ID 0x7780
73 static struct usb_device_id mcs_table[] = {
74 /* MosChip Corp., MCS7780 FIR-USB Adapter */
75 {USB_DEVICE(MCS_VENDOR_ID, MCS_PRODUCT_ID)},
76 {},
79 MODULE_AUTHOR("Brian Pugh <bpugh@cs.pdx.edu>");
80 MODULE_DESCRIPTION("IrDA-USB Dongle Driver for MosChip MCS7780");
81 MODULE_VERSION("0.3alpha");
82 MODULE_LICENSE("GPL");
84 MODULE_DEVICE_TABLE(usb, mcs_table);
86 static int qos_mtt_bits = 0x07 /* > 1ms */ ;
87 module_param(qos_mtt_bits, int, 0);
88 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
90 static int receive_mode = 0x1;
91 module_param(receive_mode, int, 0);
92 MODULE_PARM_DESC(receive_mode,
93 "Receive mode of the device (1:fast, 0:slow, default:1)");
95 static int sir_tweak = 1;
96 module_param(sir_tweak, int, 0444);
97 MODULE_PARM_DESC(sir_tweak,
98 "Default pulse width (1:1.6us, 0:3/16 bit, default:1).");
100 static int transceiver_type = MCS_TSC_VISHAY;
101 module_param(transceiver_type, int, 0444);
102 MODULE_PARM_DESC(transceiver_type, "IR transceiver type, see mcs7780.h.");
104 static struct usb_driver mcs_driver = {
105 .name = "mcs7780",
106 .probe = mcs_probe,
107 .disconnect = mcs_disconnect,
108 .id_table = mcs_table,
111 /* speed flag selection by direct addressing.
112 addr = (speed >> 8) & 0x0f
114 0x1 57600 0x2 115200 0x4 1152000 0x5 9600
115 0x6 38400 0x9 2400 0xa 576000 0xb 19200
117 4Mbps (or 2400) must be checked separately. Since it also has
118 to be programmed in a different manner that is not a big problem.
120 static __u16 mcs_speed_set[16] = { 0,
121 MCS_SPEED_57600,
122 MCS_SPEED_115200,
124 MCS_SPEED_1152000,
125 MCS_SPEED_9600,
126 MCS_SPEED_38400,
127 0, 0,
128 MCS_SPEED_2400,
129 MCS_SPEED_576000,
130 MCS_SPEED_19200,
131 0, 0, 0,
134 /* Set given 16 bit register with a 16 bit value. Send control message
135 * to set dongle register. */
136 static int mcs_set_reg(struct mcs_cb *mcs, __u16 reg, __u16 val)
138 struct usb_device *dev = mcs->usbdev;
139 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
140 MCS_WR_RTYPE, val, reg, NULL, 0,
141 msecs_to_jiffies(MCS_CTRL_TIMEOUT));
144 /* Get 16 bit register value. Send contol message to read dongle register. */
145 static int mcs_get_reg(struct mcs_cb *mcs, __u16 reg, __u16 * val)
147 struct usb_device *dev = mcs->usbdev;
148 int ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
149 MCS_RD_RTYPE, 0, reg, val, 2,
150 msecs_to_jiffies(MCS_CTRL_TIMEOUT));
152 return ret;
155 /* Setup a communication between mcs7780 and TFDU chips. It is described
156 * in more detail in the data sheet. The setup sequence puts the the
157 * vishay tranceiver into high speed mode. It will also receive SIR speed
158 * packets but at reduced sensitivity.
161 /* 0: OK 1:ERROR */
162 static inline int mcs_setup_transceiver_vishay(struct mcs_cb *mcs)
164 int ret = 0;
165 __u16 rval;
167 /* mcs_get_reg should read exactly two bytes from the dongle */
168 ret = mcs_get_reg(mcs, MCS_XCVR_REG, &rval);
169 if (unlikely(ret != 2)) {
170 ret = -EIO;
171 goto error;
174 /* The MCS_XCVR_CONF bit puts the transceiver into configuration
175 * mode. The MCS_MODE0 bit must start out high (1) and then
176 * transition to low and the MCS_STFIR and MCS_MODE1 bits must
177 * be low.
179 rval |= (MCS_MODE0 | MCS_XCVR_CONF);
180 rval &= ~MCS_STFIR;
181 rval &= ~MCS_MODE1;
182 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
183 if (unlikely(ret))
184 goto error;
186 rval &= ~MCS_MODE0;
187 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
188 if (unlikely(ret))
189 goto error;
191 rval &= ~MCS_XCVR_CONF;
192 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
193 if (unlikely(ret))
194 goto error;
196 ret = 0;
197 error:
198 return ret;
201 /* Setup a communication between mcs7780 and agilent chip. */
202 static inline int mcs_setup_transceiver_agilent(struct mcs_cb *mcs)
204 IRDA_WARNING("This transceiver type is not supported yet.");
205 return 1;
208 /* Setup a communication between mcs7780 and sharp chip. */
209 static inline int mcs_setup_transceiver_sharp(struct mcs_cb *mcs)
211 IRDA_WARNING("This transceiver type is not supported yet.");
212 return 1;
215 /* Common setup for all transceivers */
216 static inline int mcs_setup_transceiver(struct mcs_cb *mcs)
218 int ret = 0;
219 __u16 rval;
220 char *msg;
222 msg = "Basic transceiver setup error.";
224 /* read value of MODE Register, set the DRIVER and RESET bits
225 * and write value back out to MODE Register
227 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
228 if(unlikely(ret != 2))
229 goto error;
230 rval |= MCS_DRIVER; /* put the mcs7780 into configuration mode. */
231 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
232 if(unlikely(ret))
233 goto error;
235 rval = 0; /* set min pulse width to 0 initially. */
236 ret = mcs_set_reg(mcs, MCS_MINRXPW_REG, rval);
237 if(unlikely(ret))
238 goto error;
240 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
241 if(unlikely(ret != 2))
242 goto error;
244 rval &= ~MCS_FIR; /* turn off fir mode. */
245 if(mcs->sir_tweak)
246 rval |= MCS_SIR16US; /* 1.6us pulse width */
247 else
248 rval &= ~MCS_SIR16US; /* 3/16 bit time pulse width */
250 /* make sure ask mode and back to back packets are off. */
251 rval &= ~(MCS_BBTG | MCS_ASK);
253 rval &= ~MCS_SPEED_MASK;
254 rval |= MCS_SPEED_9600; /* make sure initial speed is 9600. */
255 mcs->speed = 9600;
256 mcs->new_speed = 0; /* new_speed is set to 0 */
257 rval &= ~MCS_PLLPWDN; /* disable power down. */
259 /* make sure device determines direction and that the auto send sip
260 * pulse are on.
262 rval |= MCS_DTD | MCS_SIPEN;
264 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
265 if(unlikely(ret))
266 goto error;
268 msg = "transceiver model specific setup error.";
269 switch (mcs->transceiver_type) {
270 case MCS_TSC_VISHAY:
271 ret = mcs_setup_transceiver_vishay(mcs);
272 break;
274 case MCS_TSC_SHARP:
275 ret = mcs_setup_transceiver_sharp(mcs);
276 break;
278 case MCS_TSC_AGILENT:
279 ret = mcs_setup_transceiver_agilent(mcs);
280 break;
282 default:
283 IRDA_WARNING("Unknown transceiver type: %d",
284 mcs->transceiver_type);
285 ret = 1;
287 if (unlikely(ret))
288 goto error;
290 /* If transceiver is not SHARP, then if receive mode set
291 * on the RXFAST bit in the XCVR Register otherwise unset it
293 if (mcs->transceiver_type != MCS_TSC_SHARP) {
295 ret = mcs_get_reg(mcs, MCS_XCVR_REG, &rval);
296 if (unlikely(ret != 2))
297 goto error;
298 if (mcs->receive_mode)
299 rval |= MCS_RXFAST;
300 else
301 rval &= ~MCS_RXFAST;
302 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
303 if (unlikely(ret))
304 goto error;
307 msg = "transceiver reset.";
309 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
310 if (unlikely(ret != 2))
311 goto error;
313 /* reset the mcs7780 so all changes take effect. */
314 rval &= ~MCS_RESET;
315 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
316 if (unlikely(ret))
317 goto error;
318 else
319 return ret;
321 error:
322 IRDA_ERROR("%s", msg);
323 return ret;
326 /* Wraps the data in format for SIR */
327 static inline int mcs_wrap_sir_skb(struct sk_buff *skb, __u8 * buf)
329 int wraplen;
331 /* 2: full frame length, including "the length" */
332 wraplen = async_wrap_skb(skb, buf + 2, 4094);
334 wraplen += 2;
335 buf[0] = wraplen & 0xff;
336 buf[1] = (wraplen >> 8) & 0xff;
338 return wraplen;
341 /* Wraps the data in format for FIR */
342 static unsigned mcs_wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
344 unsigned int len = 0;
345 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
347 /* add 2 bytes for length value and 4 bytes for fcs. */
348 len = skb->len + 6;
350 /* The mcs7780 requires that the first two bytes are the packet
351 * length in little endian order. Note: the length value includes
352 * the two bytes for the length value itself.
354 buf[0] = len & 0xff;
355 buf[1] = (len >> 8) & 0xff;
356 /* copy the data into the tx buffer. */
357 memcpy(buf+2, skb->data, skb->len);
358 /* put the fcs in the last four bytes in little endian order. */
359 buf[len - 4] = fcs & 0xff;
360 buf[len - 3] = (fcs >> 8) & 0xff;
361 buf[len - 2] = (fcs >> 16) & 0xff;
362 buf[len - 1] = (fcs >> 24) & 0xff;
364 return len;
367 /* Wraps the data in format for MIR */
368 static unsigned mcs_wrap_mir_skb(const struct sk_buff *skb, __u8 *buf)
370 __u16 fcs = 0;
371 int len = skb->len + 4;
373 fcs = ~(irda_calc_crc16(~fcs, skb->data, skb->len));
374 /* put the total packet length in first. Note: packet length
375 * value includes the two bytes that hold the packet length
376 * itself.
378 buf[0] = len & 0xff;
379 buf[1] = (len >> 8) & 0xff;
380 /* copy the data */
381 memcpy(buf+2, skb->data, skb->len);
382 /* put the fcs in last two bytes in little endian order. */
383 buf[len - 2] = fcs & 0xff;
384 buf[len - 1] = (fcs >> 8) & 0xff;
386 return len;
389 /* Unwrap received packets at MIR speed. A 16 bit crc_ccitt checksum is
390 * used for the fcs. When performed over the entire packet the result
391 * should be GOOD_FCS = 0xf0b8. Hands the unwrapped data off to the IrDA
392 * layer via a sk_buff.
394 static void mcs_unwrap_mir(struct mcs_cb *mcs, __u8 *buf, int len)
396 __u16 fcs;
397 int new_len;
398 struct sk_buff *skb;
400 /* Assume that the frames are going to fill a single packet
401 * rather than span multiple packets.
404 new_len = len - 2;
405 if(unlikely(new_len <= 0)) {
406 IRDA_ERROR("%s short frame length %d\n",
407 mcs->netdev->name, new_len);
408 ++mcs->stats.rx_errors;
409 ++mcs->stats.rx_length_errors;
410 return;
412 fcs = 0;
413 fcs = irda_calc_crc16(~fcs, buf, len);
415 if(fcs != GOOD_FCS) {
416 IRDA_ERROR("crc error calc 0x%x len %d\n",
417 fcs, new_len);
418 mcs->stats.rx_errors++;
419 mcs->stats.rx_crc_errors++;
420 return;
423 skb = dev_alloc_skb(new_len + 1);
424 if(unlikely(!skb)) {
425 ++mcs->stats.rx_dropped;
426 return;
429 skb_reserve(skb, 1);
430 memcpy(skb->data, buf, new_len);
431 skb_put(skb, new_len);
432 skb->mac.raw = skb->data;
433 skb->protocol = htons(ETH_P_IRDA);
434 skb->dev = mcs->netdev;
436 netif_rx(skb);
438 mcs->stats.rx_packets++;
439 mcs->stats.rx_bytes += new_len;
441 return;
444 /* Unwrap received packets at FIR speed. A 32 bit crc_ccitt checksum is
445 * used for the fcs. Hands the unwrapped data off to the IrDA
446 * layer via a sk_buff.
448 static void mcs_unwrap_fir(struct mcs_cb *mcs, __u8 *buf, int len)
450 __u32 fcs;
451 int new_len;
452 struct sk_buff *skb;
454 /* Assume that the frames are going to fill a single packet
455 * rather than span multiple packets. This is most likely a false
456 * assumption.
459 new_len = len - 4;
460 if(unlikely(new_len <= 0)) {
461 IRDA_ERROR("%s short frame length %d\n",
462 mcs->netdev->name, new_len);
463 ++mcs->stats.rx_errors;
464 ++mcs->stats.rx_length_errors;
465 return;
468 fcs = ~(crc32_le(~0, buf, new_len));
469 if(fcs != le32_to_cpu(get_unaligned((u32 *)(buf+new_len)))) {
470 IRDA_ERROR("crc error calc 0x%x len %d\n", fcs, new_len);
471 mcs->stats.rx_errors++;
472 mcs->stats.rx_crc_errors++;
473 return;
476 skb = dev_alloc_skb(new_len + 1);
477 if(unlikely(!skb)) {
478 ++mcs->stats.rx_dropped;
479 return;
482 skb_reserve(skb, 1);
483 memcpy(skb->data, buf, new_len);
484 skb_put(skb, new_len);
485 skb->mac.raw = skb->data;
486 skb->protocol = htons(ETH_P_IRDA);
487 skb->dev = mcs->netdev;
489 netif_rx(skb);
491 mcs->stats.rx_packets++;
492 mcs->stats.rx_bytes += new_len;
494 return;
498 /* Allocates urbs for both receive and transmit.
499 * If alloc fails return error code 0 (fail) otherwise
500 * return error code 1 (success).
502 static inline int mcs_setup_urbs(struct mcs_cb *mcs)
504 mcs->rx_urb = NULL;
506 mcs->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
507 if (!mcs->tx_urb)
508 return 0;
510 mcs->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
511 if (!mcs->rx_urb)
512 return 0;
514 return 1;
517 /* Sets up state to be initially outside frame, gets receive urb,
518 * sets status to successful and then submits the urb to start
519 * receiving the data.
521 static inline int mcs_receive_start(struct mcs_cb *mcs)
523 mcs->rx_buff.in_frame = FALSE;
524 mcs->rx_buff.state = OUTSIDE_FRAME;
526 usb_fill_bulk_urb(mcs->rx_urb, mcs->usbdev,
527 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_in),
528 mcs->in_buf, 4096, mcs_receive_irq, mcs);
530 mcs->rx_urb->status = 0;
531 return usb_submit_urb(mcs->rx_urb, GFP_KERNEL);
534 /* Finds the in and out endpoints for the mcs control block */
535 static inline int mcs_find_endpoints(struct mcs_cb *mcs,
536 struct usb_host_endpoint *ep, int epnum)
538 int i;
539 int ret = 0;
541 /* If no place to store the endpoints just return */
542 if (!ep)
543 return ret;
545 /* cycle through all endpoints, find the first two that are DIR_IN */
546 for (i = 0; i < epnum; i++) {
547 if (ep[i].desc.bEndpointAddress & USB_DIR_IN)
548 mcs->ep_in = ep[i].desc.bEndpointAddress;
549 else
550 mcs->ep_out = ep[i].desc.bEndpointAddress;
552 /* MosChip says that the chip has only two bulk
553 * endpoints. Find one for each direction and move on.
555 if ((mcs->ep_in != 0) && (mcs->ep_out != 0)) {
556 ret = 1;
557 break;
561 return ret;
564 static void mcs_speed_work(void *arg)
566 struct mcs_cb *mcs = arg;
567 struct net_device *netdev = mcs->netdev;
569 mcs_speed_change(mcs);
570 netif_wake_queue(netdev);
573 /* Function to change the speed of the mcs7780. Fully supports SIR,
574 * MIR, and FIR speeds.
576 static int mcs_speed_change(struct mcs_cb *mcs)
578 int ret = 0;
579 int rst = 0;
580 int cnt = 0;
581 __u16 nspeed;
582 __u16 rval;
584 nspeed = mcs_speed_set[(mcs->new_speed >> 8) & 0x0f];
586 do {
587 mcs_get_reg(mcs, MCS_RESV_REG, &rval);
588 } while(cnt++ < 100 && (rval & MCS_IRINTX));
590 if(cnt >= 100) {
591 IRDA_ERROR("unable to change speed");
592 ret = -EIO;
593 goto error;
596 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
598 /* MINRXPW values recomended by MosChip */
599 if (mcs->new_speed <= 115200) {
600 rval &= ~MCS_FIR;
602 if ((rst = (mcs->speed > 115200)))
603 mcs_set_reg(mcs, MCS_MINRXPW_REG, 0);
605 } else if (mcs->new_speed <= 1152000) {
606 rval &= ~MCS_FIR;
608 if ((rst = !(mcs->speed == 576000 || mcs->speed == 1152000)))
609 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
611 } else {
612 rval |= MCS_FIR;
614 if ((rst = (mcs->speed != 4000000)))
615 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
619 rval &= ~MCS_SPEED_MASK;
620 rval |= nspeed;
622 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
623 if (unlikely(ret))
624 goto error;
626 if (rst)
627 switch (mcs->transceiver_type) {
628 case MCS_TSC_VISHAY:
629 ret = mcs_setup_transceiver_vishay(mcs);
630 break;
632 case MCS_TSC_SHARP:
633 ret = mcs_setup_transceiver_sharp(mcs);
634 break;
636 case MCS_TSC_AGILENT:
637 ret = mcs_setup_transceiver_agilent(mcs);
638 break;
640 default:
641 ret = 1;
642 IRDA_WARNING("Unknown transceiver type: %d",
643 mcs->transceiver_type);
645 if (unlikely(ret))
646 goto error;
648 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
649 rval &= ~MCS_RESET;
650 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
652 mcs->speed = mcs->new_speed;
653 error:
654 mcs->new_speed = 0;
655 return ret;
658 /* Ioctl calls not supported at this time. Can be an area of future work. */
659 static int mcs_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
661 /* struct if_irda_req *irq = (struct if_irda_req *)rq; */
662 /* struct mcs_cb *mcs = netdev_priv(netdev); */
663 int ret = 0;
665 switch (cmd) {
666 default:
667 ret = -EOPNOTSUPP;
670 return ret;
673 /* Network device is taken down, done by "ifconfig irda0 down" */
674 static int mcs_net_close(struct net_device *netdev)
676 int ret = 0;
677 struct mcs_cb *mcs = netdev_priv(netdev);
679 /* Stop transmit processing */
680 netif_stop_queue(netdev);
682 /* kill and free the receive and transmit URBs */
683 usb_kill_urb(mcs->rx_urb);
684 usb_free_urb(mcs->rx_urb);
685 usb_kill_urb(mcs->tx_urb);
686 usb_free_urb(mcs->tx_urb);
688 /* Stop and remove instance of IrLAP */
689 if (mcs->irlap)
690 irlap_close(mcs->irlap);
692 mcs->irlap = NULL;
693 return ret;
696 /* Network device is taken up, done by "ifconfig irda0 up" */
697 static int mcs_net_open(struct net_device *netdev)
699 struct mcs_cb *mcs = netdev_priv(netdev);
700 char hwname[16];
701 int ret = 0;
703 ret = usb_clear_halt(mcs->usbdev,
704 usb_sndbulkpipe(mcs->usbdev, mcs->ep_in));
705 if (ret)
706 goto error1;
707 ret = usb_clear_halt(mcs->usbdev,
708 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_out));
709 if (ret)
710 goto error1;
712 ret = mcs_setup_transceiver(mcs);
713 if (ret)
714 goto error1;
716 ret = -ENOMEM;
718 /* Initialize for SIR/FIR to copy data directly into skb. */
719 mcs->receiving = 0;
720 mcs->rx_buff.truesize = IRDA_SKB_MAX_MTU;
721 mcs->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
722 if (!mcs->rx_buff.skb)
723 goto error1;
725 skb_reserve(mcs->rx_buff.skb, 1);
726 mcs->rx_buff.head = mcs->rx_buff.skb->data;
727 do_gettimeofday(&mcs->rx_time);
730 * Now that everything should be initialized properly,
731 * Open new IrLAP layer instance to take care of us...
732 * Note : will send immediately a speed change...
734 sprintf(hwname, "usb#%d", mcs->usbdev->devnum);
735 mcs->irlap = irlap_open(netdev, &mcs->qos, hwname);
736 if (!mcs->irlap) {
737 IRDA_ERROR("mcs7780: irlap_open failed");
738 goto error2;
741 if (!mcs_setup_urbs(mcs))
742 goto error3;
744 ret = mcs_receive_start(mcs);
745 if (ret)
746 goto error3;
748 netif_start_queue(netdev);
749 return 0;
751 error3:
752 irlap_close(mcs->irlap);
753 error2:
754 kfree_skb(mcs->rx_buff.skb);
755 error1:
756 return ret;
760 /* Get device stats for /proc/net/dev and ifconfig */
761 static struct net_device_stats *mcs_net_get_stats(struct net_device *netdev)
763 struct mcs_cb *mcs = netdev_priv(netdev);
764 return &mcs->stats;
767 /* Receive callback function. */
768 static void mcs_receive_irq(struct urb *urb, struct pt_regs *regs)
770 __u8 *bytes;
771 struct mcs_cb *mcs = urb->context;
772 int i;
773 int ret;
775 if (!netif_running(mcs->netdev))
776 return;
778 if (urb->status)
779 return;
781 if (urb->actual_length > 0) {
782 bytes = urb->transfer_buffer;
784 /* MCS returns frames without BOF and EOF
785 * I assume it returns whole frames.
787 /* SIR speed */
788 if(mcs->speed < 576000) {
789 async_unwrap_char(mcs->netdev, &mcs->stats,
790 &mcs->rx_buff, 0xc0);
792 for (i = 0; i < urb->actual_length; i++)
793 async_unwrap_char(mcs->netdev, &mcs->stats,
794 &mcs->rx_buff, bytes[i]);
796 async_unwrap_char(mcs->netdev, &mcs->stats,
797 &mcs->rx_buff, 0xc1);
799 /* MIR speed */
800 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
801 mcs_unwrap_mir(mcs, urb->transfer_buffer,
802 urb->actual_length);
804 /* FIR speed */
805 else {
806 mcs_unwrap_fir(mcs, urb->transfer_buffer,
807 urb->actual_length);
809 mcs->netdev->last_rx = jiffies;
810 do_gettimeofday(&mcs->rx_time);
813 ret = usb_submit_urb(urb, GFP_ATOMIC);
816 /* Transmit callback funtion. */
817 static void mcs_send_irq(struct urb *urb, struct pt_regs *regs)
819 struct mcs_cb *mcs = urb->context;
820 struct net_device *ndev = mcs->netdev;
822 if (unlikely(mcs->new_speed))
823 schedule_work(&mcs->work);
824 else
825 netif_wake_queue(ndev);
828 /* Transmit callback funtion. */
829 static int mcs_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
831 unsigned long flags;
832 struct mcs_cb *mcs;
833 int wraplen;
834 int ret = 0;
837 if (skb == NULL || ndev == NULL)
838 return -EINVAL;
840 netif_stop_queue(ndev);
841 mcs = netdev_priv(ndev);
843 spin_lock_irqsave(&mcs->lock, flags);
845 mcs->new_speed = irda_get_next_speed(skb);
846 if (likely(mcs->new_speed == mcs->speed))
847 mcs->new_speed = 0;
849 /* SIR speed */
850 if(mcs->speed < 576000) {
851 wraplen = mcs_wrap_sir_skb(skb, mcs->out_buf);
853 /* MIR speed */
854 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
855 wraplen = mcs_wrap_mir_skb(skb, mcs->out_buf);
857 /* FIR speed */
858 else {
859 wraplen = mcs_wrap_fir_skb(skb, mcs->out_buf);
861 usb_fill_bulk_urb(mcs->tx_urb, mcs->usbdev,
862 usb_sndbulkpipe(mcs->usbdev, mcs->ep_out),
863 mcs->out_buf, wraplen, mcs_send_irq, mcs);
865 if ((ret = usb_submit_urb(mcs->tx_urb, GFP_ATOMIC))) {
866 IRDA_ERROR("failed tx_urb: %d", ret);
867 switch (ret) {
868 case -ENODEV:
869 case -EPIPE:
870 break;
871 default:
872 mcs->stats.tx_errors++;
873 netif_start_queue(ndev);
875 } else {
876 mcs->stats.tx_packets++;
877 mcs->stats.tx_bytes += skb->len;
880 dev_kfree_skb(skb);
881 spin_unlock_irqrestore(&mcs->lock, flags);
882 return ret;
886 * This function is called by the USB subsystem for each new device in the
887 * system. Need to verify the device and if it is, then start handling it.
889 static int mcs_probe(struct usb_interface *intf,
890 const struct usb_device_id *id)
892 struct usb_device *udev = interface_to_usbdev(intf);
893 struct net_device *ndev = NULL;
894 struct mcs_cb *mcs;
895 int ret = -ENOMEM;
897 ndev = alloc_irdadev(sizeof(*mcs));
898 if (!ndev)
899 goto error1;
901 IRDA_DEBUG(1, "MCS7780 USB-IrDA bridge found at %d.", udev->devnum);
903 /* what is it realy for? */
904 SET_MODULE_OWNER(ndev);
905 SET_NETDEV_DEV(ndev, &intf->dev);
907 ret = usb_reset_configuration(udev);
908 if (ret != 0) {
909 IRDA_ERROR("mcs7780: usb reset configuration failed");
910 goto error2;
913 mcs = netdev_priv(ndev);
914 mcs->usbdev = udev;
915 mcs->netdev = ndev;
916 spin_lock_init(&mcs->lock);
918 /* Initialize QoS for this device */
919 irda_init_max_qos_capabilies(&mcs->qos);
921 /* That's the Rx capability. */
922 mcs->qos.baud_rate.bits &=
923 IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200
924 | IR_576000 | IR_1152000 | (IR_4000000 << 8);
927 mcs->qos.min_turn_time.bits &= qos_mtt_bits;
928 irda_qos_bits_to_value(&mcs->qos);
930 /* Speed change work initialisation*/
931 INIT_WORK(&mcs->work, mcs_speed_work, mcs);
933 /* Override the network functions we need to use */
934 ndev->hard_start_xmit = mcs_hard_xmit;
935 ndev->open = mcs_net_open;
936 ndev->stop = mcs_net_close;
937 ndev->get_stats = mcs_net_get_stats;
938 ndev->do_ioctl = mcs_net_ioctl;
940 if (!intf->cur_altsetting)
941 goto error2;
943 ret = mcs_find_endpoints(mcs, intf->cur_altsetting->endpoint,
944 intf->cur_altsetting->desc.bNumEndpoints);
945 if (!ret) {
946 ret = -ENODEV;
947 goto error2;
950 ret = register_netdev(ndev);
951 if (ret != 0)
952 goto error2;
954 IRDA_DEBUG(1, "IrDA: Registered MosChip MCS7780 device as %s",
955 ndev->name);
957 mcs->transceiver_type = transceiver_type;
958 mcs->sir_tweak = sir_tweak;
959 mcs->receive_mode = receive_mode;
961 usb_set_intfdata(intf, mcs);
962 return 0;
964 error2:
965 free_netdev(ndev);
967 error1:
968 return ret;
971 /* The current device is removed, the USB layer tells us to shut down. */
972 static void mcs_disconnect(struct usb_interface *intf)
974 struct mcs_cb *mcs = usb_get_intfdata(intf);
976 if (!mcs)
977 return;
979 flush_scheduled_work();
981 unregister_netdev(mcs->netdev);
982 free_netdev(mcs->netdev);
984 usb_set_intfdata(intf, NULL);
985 IRDA_DEBUG(0, "MCS7780 now disconnected.");
988 /* Module insertion */
989 static int __init mcs_init(void)
991 int result;
993 /* register this driver with the USB subsystem */
994 result = usb_register(&mcs_driver);
995 if (result)
996 IRDA_ERROR("usb_register failed. Error number %d", result);
998 return result;
1000 module_init(mcs_init);
1002 /* Module removal */
1003 static void __exit mcs_exit(void)
1005 /* deregister this driver with the USB subsystem */
1006 usb_deregister(&mcs_driver);
1008 module_exit(mcs_exit);