blob: f0c61f3b2a82e992fee61cdf664dd2d42e62cfdc [file] [log] [blame]
Samuel Ortizc6ae5222006-05-25 16:19:22 -07001/*****************************************************************************
2*
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>
9*
10* Based on stir4200 driver, but some things done differently.
11* Based on earlier driver by Paul Stewart <stewart@parc.com>
12*
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>
20*
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.
25*
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.
30*
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.
34*
35*****************************************************************************/
36
37/*
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
44 */
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
Samuel Ortizc6ae5222006-05-25 16:19:22 -070048#include <linux/kernel.h>
49#include <linux/types.h>
50#include <linux/errno.h>
51#include <linux/init.h>
52#include <linux/slab.h>
53#include <linux/module.h>
54#include <linux/kref.h>
55#include <linux/usb.h>
56#include <linux/device.h>
57#include <linux/crc32.h>
58
59#include <asm/unaligned.h>
60#include <asm/byteorder.h>
61#include <asm/uaccess.h>
62
63#include <net/irda/irda.h>
64#include <net/irda/wrapper.h>
65#include <net/irda/crc.h>
66
67#include "mcs7780.h"
68
69#define MCS_VENDOR_ID 0x9710
70#define MCS_PRODUCT_ID 0x7780
71
72static struct usb_device_id mcs_table[] = {
73 /* MosChip Corp., MCS7780 FIR-USB Adapter */
74 {USB_DEVICE(MCS_VENDOR_ID, MCS_PRODUCT_ID)},
75 {},
76};
77
78MODULE_AUTHOR("Brian Pugh <bpugh@cs.pdx.edu>");
79MODULE_DESCRIPTION("IrDA-USB Dongle Driver for MosChip MCS7780");
80MODULE_VERSION("0.3alpha");
81MODULE_LICENSE("GPL");
82
83MODULE_DEVICE_TABLE(usb, mcs_table);
84
85static int qos_mtt_bits = 0x07 /* > 1ms */ ;
86module_param(qos_mtt_bits, int, 0);
87MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
88
89static int receive_mode = 0x1;
90module_param(receive_mode, int, 0);
91MODULE_PARM_DESC(receive_mode,
92 "Receive mode of the device (1:fast, 0:slow, default:1)");
93
94static int sir_tweak = 1;
95module_param(sir_tweak, int, 0444);
96MODULE_PARM_DESC(sir_tweak,
97 "Default pulse width (1:1.6us, 0:3/16 bit, default:1).");
98
99static int transceiver_type = MCS_TSC_VISHAY;
100module_param(transceiver_type, int, 0444);
101MODULE_PARM_DESC(transceiver_type, "IR transceiver type, see mcs7780.h.");
102
Adrian Bunk7263ade2006-06-29 12:39:07 -0700103static struct usb_driver mcs_driver = {
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700104 .name = "mcs7780",
105 .probe = mcs_probe,
106 .disconnect = mcs_disconnect,
107 .id_table = mcs_table,
108};
109
110/* speed flag selection by direct addressing.
111addr = (speed >> 8) & 0x0f
112
1130x1 57600 0x2 115200 0x4 1152000 0x5 9600
1140x6 38400 0x9 2400 0xa 576000 0xb 19200
115
1164Mbps (or 2400) must be checked separately. Since it also has
117to be programmed in a different manner that is not a big problem.
118*/
119static __u16 mcs_speed_set[16] = { 0,
120 MCS_SPEED_57600,
121 MCS_SPEED_115200,
122 0,
123 MCS_SPEED_1152000,
124 MCS_SPEED_9600,
125 MCS_SPEED_38400,
126 0, 0,
127 MCS_SPEED_2400,
128 MCS_SPEED_576000,
129 MCS_SPEED_19200,
130 0, 0, 0,
131};
132
133/* Set given 16 bit register with a 16 bit value. Send control message
134 * to set dongle register. */
135static int mcs_set_reg(struct mcs_cb *mcs, __u16 reg, __u16 val)
136{
137 struct usb_device *dev = mcs->usbdev;
138 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
139 MCS_WR_RTYPE, val, reg, NULL, 0,
140 msecs_to_jiffies(MCS_CTRL_TIMEOUT));
141}
142
143/* Get 16 bit register value. Send contol message to read dongle register. */
144static int mcs_get_reg(struct mcs_cb *mcs, __u16 reg, __u16 * val)
145{
146 struct usb_device *dev = mcs->usbdev;
147 int ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
148 MCS_RD_RTYPE, 0, reg, val, 2,
149 msecs_to_jiffies(MCS_CTRL_TIMEOUT));
150
151 return ret;
152}
153
154/* Setup a communication between mcs7780 and TFDU chips. It is described
155 * in more detail in the data sheet. The setup sequence puts the the
156 * vishay tranceiver into high speed mode. It will also receive SIR speed
157 * packets but at reduced sensitivity.
158 */
159
160/* 0: OK 1:ERROR */
161static inline int mcs_setup_transceiver_vishay(struct mcs_cb *mcs)
162{
163 int ret = 0;
164 __u16 rval;
165
166 /* mcs_get_reg should read exactly two bytes from the dongle */
167 ret = mcs_get_reg(mcs, MCS_XCVR_REG, &rval);
168 if (unlikely(ret != 2)) {
169 ret = -EIO;
170 goto error;
171 }
172
173 /* The MCS_XCVR_CONF bit puts the transceiver into configuration
174 * mode. The MCS_MODE0 bit must start out high (1) and then
175 * transition to low and the MCS_STFIR and MCS_MODE1 bits must
176 * be low.
177 */
178 rval |= (MCS_MODE0 | MCS_XCVR_CONF);
179 rval &= ~MCS_STFIR;
180 rval &= ~MCS_MODE1;
181 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
182 if (unlikely(ret))
183 goto error;
184
185 rval &= ~MCS_MODE0;
186 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
187 if (unlikely(ret))
188 goto error;
189
190 rval &= ~MCS_XCVR_CONF;
191 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
192 if (unlikely(ret))
193 goto error;
194
195 ret = 0;
196 error:
197 return ret;
198}
199
200/* Setup a communication between mcs7780 and agilent chip. */
201static inline int mcs_setup_transceiver_agilent(struct mcs_cb *mcs)
202{
203 IRDA_WARNING("This transceiver type is not supported yet.");
204 return 1;
205}
206
207/* Setup a communication between mcs7780 and sharp chip. */
208static inline int mcs_setup_transceiver_sharp(struct mcs_cb *mcs)
209{
210 IRDA_WARNING("This transceiver type is not supported yet.");
211 return 1;
212}
213
214/* Common setup for all transceivers */
215static inline int mcs_setup_transceiver(struct mcs_cb *mcs)
216{
217 int ret = 0;
218 __u16 rval;
219 char *msg;
220
221 msg = "Basic transceiver setup error.";
222
223 /* read value of MODE Register, set the DRIVER and RESET bits
224 * and write value back out to MODE Register
225 */
226 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
227 if(unlikely(ret != 2))
228 goto error;
229 rval |= MCS_DRIVER; /* put the mcs7780 into configuration mode. */
230 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
231 if(unlikely(ret))
232 goto error;
233
234 rval = 0; /* set min pulse width to 0 initially. */
235 ret = mcs_set_reg(mcs, MCS_MINRXPW_REG, rval);
236 if(unlikely(ret))
237 goto error;
238
239 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
240 if(unlikely(ret != 2))
241 goto error;
242
243 rval &= ~MCS_FIR; /* turn off fir mode. */
244 if(mcs->sir_tweak)
245 rval |= MCS_SIR16US; /* 1.6us pulse width */
246 else
247 rval &= ~MCS_SIR16US; /* 3/16 bit time pulse width */
248
249 /* make sure ask mode and back to back packets are off. */
250 rval &= ~(MCS_BBTG | MCS_ASK);
251
252 rval &= ~MCS_SPEED_MASK;
253 rval |= MCS_SPEED_9600; /* make sure initial speed is 9600. */
254 mcs->speed = 9600;
255 mcs->new_speed = 0; /* new_speed is set to 0 */
256 rval &= ~MCS_PLLPWDN; /* disable power down. */
257
258 /* make sure device determines direction and that the auto send sip
259 * pulse are on.
260 */
261 rval |= MCS_DTD | MCS_SIPEN;
262
263 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
264 if(unlikely(ret))
265 goto error;
266
267 msg = "transceiver model specific setup error.";
268 switch (mcs->transceiver_type) {
269 case MCS_TSC_VISHAY:
270 ret = mcs_setup_transceiver_vishay(mcs);
271 break;
272
273 case MCS_TSC_SHARP:
274 ret = mcs_setup_transceiver_sharp(mcs);
275 break;
276
277 case MCS_TSC_AGILENT:
278 ret = mcs_setup_transceiver_agilent(mcs);
279 break;
280
281 default:
282 IRDA_WARNING("Unknown transceiver type: %d",
283 mcs->transceiver_type);
284 ret = 1;
285 }
286 if (unlikely(ret))
287 goto error;
288
289 /* If transceiver is not SHARP, then if receive mode set
290 * on the RXFAST bit in the XCVR Register otherwise unset it
291 */
292 if (mcs->transceiver_type != MCS_TSC_SHARP) {
293
294 ret = mcs_get_reg(mcs, MCS_XCVR_REG, &rval);
295 if (unlikely(ret != 2))
296 goto error;
297 if (mcs->receive_mode)
298 rval |= MCS_RXFAST;
299 else
300 rval &= ~MCS_RXFAST;
301 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
302 if (unlikely(ret))
303 goto error;
304 }
305
306 msg = "transceiver reset.";
307
308 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
309 if (unlikely(ret != 2))
310 goto error;
311
312 /* reset the mcs7780 so all changes take effect. */
313 rval &= ~MCS_RESET;
314 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
315 if (unlikely(ret))
316 goto error;
317 else
318 return ret;
319
320error:
321 IRDA_ERROR("%s", msg);
322 return ret;
323}
324
325/* Wraps the data in format for SIR */
326static inline int mcs_wrap_sir_skb(struct sk_buff *skb, __u8 * buf)
327{
328 int wraplen;
329
330 /* 2: full frame length, including "the length" */
331 wraplen = async_wrap_skb(skb, buf + 2, 4094);
332
333 wraplen += 2;
334 buf[0] = wraplen & 0xff;
335 buf[1] = (wraplen >> 8) & 0xff;
336
337 return wraplen;
338}
339
340/* Wraps the data in format for FIR */
341static unsigned mcs_wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
342{
343 unsigned int len = 0;
344 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
345
346 /* add 2 bytes for length value and 4 bytes for fcs. */
347 len = skb->len + 6;
348
349 /* The mcs7780 requires that the first two bytes are the packet
350 * length in little endian order. Note: the length value includes
351 * the two bytes for the length value itself.
352 */
353 buf[0] = len & 0xff;
354 buf[1] = (len >> 8) & 0xff;
355 /* copy the data into the tx buffer. */
356 memcpy(buf+2, skb->data, skb->len);
357 /* put the fcs in the last four bytes in little endian order. */
358 buf[len - 4] = fcs & 0xff;
359 buf[len - 3] = (fcs >> 8) & 0xff;
360 buf[len - 2] = (fcs >> 16) & 0xff;
361 buf[len - 1] = (fcs >> 24) & 0xff;
362
363 return len;
364}
365
366/* Wraps the data in format for MIR */
367static unsigned mcs_wrap_mir_skb(const struct sk_buff *skb, __u8 *buf)
368{
369 __u16 fcs = 0;
370 int len = skb->len + 4;
371
372 fcs = ~(irda_calc_crc16(~fcs, skb->data, skb->len));
373 /* put the total packet length in first. Note: packet length
374 * value includes the two bytes that hold the packet length
375 * itself.
376 */
377 buf[0] = len & 0xff;
378 buf[1] = (len >> 8) & 0xff;
379 /* copy the data */
380 memcpy(buf+2, skb->data, skb->len);
381 /* put the fcs in last two bytes in little endian order. */
382 buf[len - 2] = fcs & 0xff;
383 buf[len - 1] = (fcs >> 8) & 0xff;
384
385 return len;
386}
387
388/* Unwrap received packets at MIR speed. A 16 bit crc_ccitt checksum is
389 * used for the fcs. When performed over the entire packet the result
390 * should be GOOD_FCS = 0xf0b8. Hands the unwrapped data off to the IrDA
391 * layer via a sk_buff.
392 */
393static void mcs_unwrap_mir(struct mcs_cb *mcs, __u8 *buf, int len)
394{
395 __u16 fcs;
396 int new_len;
397 struct sk_buff *skb;
398
399 /* Assume that the frames are going to fill a single packet
400 * rather than span multiple packets.
401 */
402
403 new_len = len - 2;
404 if(unlikely(new_len <= 0)) {
405 IRDA_ERROR("%s short frame length %d\n",
406 mcs->netdev->name, new_len);
407 ++mcs->stats.rx_errors;
408 ++mcs->stats.rx_length_errors;
409 return;
410 }
411 fcs = 0;
412 fcs = irda_calc_crc16(~fcs, buf, len);
413
414 if(fcs != GOOD_FCS) {
415 IRDA_ERROR("crc error calc 0x%x len %d\n",
416 fcs, new_len);
417 mcs->stats.rx_errors++;
418 mcs->stats.rx_crc_errors++;
419 return;
420 }
421
422 skb = dev_alloc_skb(new_len + 1);
423 if(unlikely(!skb)) {
424 ++mcs->stats.rx_dropped;
425 return;
426 }
427
428 skb_reserve(skb, 1);
429 memcpy(skb->data, buf, new_len);
430 skb_put(skb, new_len);
431 skb->mac.raw = skb->data;
432 skb->protocol = htons(ETH_P_IRDA);
433 skb->dev = mcs->netdev;
434
435 netif_rx(skb);
436
437 mcs->stats.rx_packets++;
438 mcs->stats.rx_bytes += new_len;
439
440 return;
441}
442
443/* Unwrap received packets at FIR speed. A 32 bit crc_ccitt checksum is
444 * used for the fcs. Hands the unwrapped data off to the IrDA
445 * layer via a sk_buff.
446 */
447static void mcs_unwrap_fir(struct mcs_cb *mcs, __u8 *buf, int len)
448{
449 __u32 fcs;
450 int new_len;
451 struct sk_buff *skb;
452
453 /* Assume that the frames are going to fill a single packet
454 * rather than span multiple packets. This is most likely a false
455 * assumption.
456 */
457
458 new_len = len - 4;
459 if(unlikely(new_len <= 0)) {
460 IRDA_ERROR("%s short frame length %d\n",
461 mcs->netdev->name, new_len);
462 ++mcs->stats.rx_errors;
463 ++mcs->stats.rx_length_errors;
464 return;
465 }
466
467 fcs = ~(crc32_le(~0, buf, new_len));
468 if(fcs != le32_to_cpu(get_unaligned((u32 *)(buf+new_len)))) {
469 IRDA_ERROR("crc error calc 0x%x len %d\n", fcs, new_len);
470 mcs->stats.rx_errors++;
471 mcs->stats.rx_crc_errors++;
472 return;
473 }
474
475 skb = dev_alloc_skb(new_len + 1);
476 if(unlikely(!skb)) {
477 ++mcs->stats.rx_dropped;
478 return;
479 }
480
481 skb_reserve(skb, 1);
482 memcpy(skb->data, buf, new_len);
483 skb_put(skb, new_len);
484 skb->mac.raw = skb->data;
485 skb->protocol = htons(ETH_P_IRDA);
486 skb->dev = mcs->netdev;
487
488 netif_rx(skb);
489
490 mcs->stats.rx_packets++;
491 mcs->stats.rx_bytes += new_len;
492
493 return;
494}
495
496
497/* Allocates urbs for both receive and transmit.
498 * If alloc fails return error code 0 (fail) otherwise
499 * return error code 1 (success).
500 */
501static inline int mcs_setup_urbs(struct mcs_cb *mcs)
502{
503 mcs->rx_urb = NULL;
504
505 mcs->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
506 if (!mcs->tx_urb)
507 return 0;
508
509 mcs->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
510 if (!mcs->rx_urb)
511 return 0;
512
513 return 1;
514}
515
516/* Sets up state to be initially outside frame, gets receive urb,
517 * sets status to successful and then submits the urb to start
518 * receiving the data.
519 */
520static inline int mcs_receive_start(struct mcs_cb *mcs)
521{
522 mcs->rx_buff.in_frame = FALSE;
523 mcs->rx_buff.state = OUTSIDE_FRAME;
524
525 usb_fill_bulk_urb(mcs->rx_urb, mcs->usbdev,
526 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_in),
527 mcs->in_buf, 4096, mcs_receive_irq, mcs);
528
529 mcs->rx_urb->status = 0;
530 return usb_submit_urb(mcs->rx_urb, GFP_KERNEL);
531}
532
533/* Finds the in and out endpoints for the mcs control block */
534static inline int mcs_find_endpoints(struct mcs_cb *mcs,
535 struct usb_host_endpoint *ep, int epnum)
536{
537 int i;
538 int ret = 0;
539
540 /* If no place to store the endpoints just return */
541 if (!ep)
542 return ret;
543
544 /* cycle through all endpoints, find the first two that are DIR_IN */
545 for (i = 0; i < epnum; i++) {
546 if (ep[i].desc.bEndpointAddress & USB_DIR_IN)
547 mcs->ep_in = ep[i].desc.bEndpointAddress;
548 else
549 mcs->ep_out = ep[i].desc.bEndpointAddress;
550
551 /* MosChip says that the chip has only two bulk
552 * endpoints. Find one for each direction and move on.
553 */
554 if ((mcs->ep_in != 0) && (mcs->ep_out != 0)) {
555 ret = 1;
556 break;
557 }
558 }
559
560 return ret;
561}
562
David Howellsc4028952006-11-22 14:57:56 +0000563static void mcs_speed_work(struct work_struct *work)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700564{
David Howellsc4028952006-11-22 14:57:56 +0000565 struct mcs_cb *mcs = container_of(work, struct mcs_cb, work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700566 struct net_device *netdev = mcs->netdev;
567
568 mcs_speed_change(mcs);
569 netif_wake_queue(netdev);
570}
571
572/* Function to change the speed of the mcs7780. Fully supports SIR,
573 * MIR, and FIR speeds.
574 */
575static int mcs_speed_change(struct mcs_cb *mcs)
576{
577 int ret = 0;
578 int rst = 0;
579 int cnt = 0;
580 __u16 nspeed;
581 __u16 rval;
582
583 nspeed = mcs_speed_set[(mcs->new_speed >> 8) & 0x0f];
584
585 do {
586 mcs_get_reg(mcs, MCS_RESV_REG, &rval);
587 } while(cnt++ < 100 && (rval & MCS_IRINTX));
588
589 if(cnt >= 100) {
590 IRDA_ERROR("unable to change speed");
591 ret = -EIO;
592 goto error;
593 }
594
595 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
596
597 /* MINRXPW values recomended by MosChip */
598 if (mcs->new_speed <= 115200) {
599 rval &= ~MCS_FIR;
600
601 if ((rst = (mcs->speed > 115200)))
602 mcs_set_reg(mcs, MCS_MINRXPW_REG, 0);
603
604 } else if (mcs->new_speed <= 1152000) {
605 rval &= ~MCS_FIR;
606
607 if ((rst = !(mcs->speed == 576000 || mcs->speed == 1152000)))
608 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
609
610 } else {
611 rval |= MCS_FIR;
612
613 if ((rst = (mcs->speed != 4000000)))
614 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
615
616 }
617
618 rval &= ~MCS_SPEED_MASK;
619 rval |= nspeed;
620
621 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
622 if (unlikely(ret))
623 goto error;
624
625 if (rst)
626 switch (mcs->transceiver_type) {
627 case MCS_TSC_VISHAY:
628 ret = mcs_setup_transceiver_vishay(mcs);
629 break;
630
631 case MCS_TSC_SHARP:
632 ret = mcs_setup_transceiver_sharp(mcs);
633 break;
634
635 case MCS_TSC_AGILENT:
636 ret = mcs_setup_transceiver_agilent(mcs);
637 break;
638
639 default:
640 ret = 1;
641 IRDA_WARNING("Unknown transceiver type: %d",
642 mcs->transceiver_type);
643 }
644 if (unlikely(ret))
645 goto error;
646
647 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
648 rval &= ~MCS_RESET;
649 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
650
651 mcs->speed = mcs->new_speed;
652 error:
653 mcs->new_speed = 0;
654 return ret;
655}
656
657/* Ioctl calls not supported at this time. Can be an area of future work. */
658static int mcs_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
659{
660 /* struct if_irda_req *irq = (struct if_irda_req *)rq; */
661 /* struct mcs_cb *mcs = netdev_priv(netdev); */
662 int ret = 0;
663
664 switch (cmd) {
665 default:
666 ret = -EOPNOTSUPP;
667 }
668
669 return ret;
670}
671
672/* Network device is taken down, done by "ifconfig irda0 down" */
673static int mcs_net_close(struct net_device *netdev)
674{
675 int ret = 0;
676 struct mcs_cb *mcs = netdev_priv(netdev);
677
678 /* Stop transmit processing */
679 netif_stop_queue(netdev);
680
681 /* kill and free the receive and transmit URBs */
682 usb_kill_urb(mcs->rx_urb);
683 usb_free_urb(mcs->rx_urb);
684 usb_kill_urb(mcs->tx_urb);
685 usb_free_urb(mcs->tx_urb);
686
687 /* Stop and remove instance of IrLAP */
688 if (mcs->irlap)
689 irlap_close(mcs->irlap);
690
691 mcs->irlap = NULL;
692 return ret;
693}
694
695/* Network device is taken up, done by "ifconfig irda0 up" */
696static int mcs_net_open(struct net_device *netdev)
697{
698 struct mcs_cb *mcs = netdev_priv(netdev);
699 char hwname[16];
700 int ret = 0;
701
702 ret = usb_clear_halt(mcs->usbdev,
703 usb_sndbulkpipe(mcs->usbdev, mcs->ep_in));
704 if (ret)
705 goto error1;
706 ret = usb_clear_halt(mcs->usbdev,
707 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_out));
708 if (ret)
709 goto error1;
710
711 ret = mcs_setup_transceiver(mcs);
712 if (ret)
713 goto error1;
714
715 ret = -ENOMEM;
716
717 /* Initialize for SIR/FIR to copy data directly into skb. */
718 mcs->receiving = 0;
719 mcs->rx_buff.truesize = IRDA_SKB_MAX_MTU;
720 mcs->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
721 if (!mcs->rx_buff.skb)
722 goto error1;
723
724 skb_reserve(mcs->rx_buff.skb, 1);
725 mcs->rx_buff.head = mcs->rx_buff.skb->data;
726 do_gettimeofday(&mcs->rx_time);
727
728 /*
729 * Now that everything should be initialized properly,
730 * Open new IrLAP layer instance to take care of us...
731 * Note : will send immediately a speed change...
732 */
733 sprintf(hwname, "usb#%d", mcs->usbdev->devnum);
734 mcs->irlap = irlap_open(netdev, &mcs->qos, hwname);
735 if (!mcs->irlap) {
736 IRDA_ERROR("mcs7780: irlap_open failed");
737 goto error2;
738 }
739
740 if (!mcs_setup_urbs(mcs))
741 goto error3;
742
743 ret = mcs_receive_start(mcs);
744 if (ret)
745 goto error3;
746
747 netif_start_queue(netdev);
748 return 0;
749
750 error3:
751 irlap_close(mcs->irlap);
752 error2:
753 kfree_skb(mcs->rx_buff.skb);
754 error1:
755 return ret;
756}
757
758
759/* Get device stats for /proc/net/dev and ifconfig */
760static struct net_device_stats *mcs_net_get_stats(struct net_device *netdev)
761{
762 struct mcs_cb *mcs = netdev_priv(netdev);
763 return &mcs->stats;
764}
765
766/* Receive callback function. */
David Howells7d12e782006-10-05 14:55:46 +0100767static void mcs_receive_irq(struct urb *urb)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700768{
769 __u8 *bytes;
770 struct mcs_cb *mcs = urb->context;
771 int i;
772 int ret;
773
774 if (!netif_running(mcs->netdev))
775 return;
776
777 if (urb->status)
778 return;
779
780 if (urb->actual_length > 0) {
781 bytes = urb->transfer_buffer;
782
783 /* MCS returns frames without BOF and EOF
784 * I assume it returns whole frames.
785 */
786 /* SIR speed */
787 if(mcs->speed < 576000) {
788 async_unwrap_char(mcs->netdev, &mcs->stats,
789 &mcs->rx_buff, 0xc0);
790
791 for (i = 0; i < urb->actual_length; i++)
792 async_unwrap_char(mcs->netdev, &mcs->stats,
793 &mcs->rx_buff, bytes[i]);
794
795 async_unwrap_char(mcs->netdev, &mcs->stats,
796 &mcs->rx_buff, 0xc1);
797 }
798 /* MIR speed */
799 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
800 mcs_unwrap_mir(mcs, urb->transfer_buffer,
801 urb->actual_length);
802 }
803 /* FIR speed */
804 else {
805 mcs_unwrap_fir(mcs, urb->transfer_buffer,
806 urb->actual_length);
807 }
808 mcs->netdev->last_rx = jiffies;
809 do_gettimeofday(&mcs->rx_time);
810 }
811
812 ret = usb_submit_urb(urb, GFP_ATOMIC);
813}
814
815/* Transmit callback funtion. */
David Howells7d12e782006-10-05 14:55:46 +0100816static void mcs_send_irq(struct urb *urb)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700817{
818 struct mcs_cb *mcs = urb->context;
819 struct net_device *ndev = mcs->netdev;
820
821 if (unlikely(mcs->new_speed))
822 schedule_work(&mcs->work);
823 else
824 netif_wake_queue(ndev);
825}
826
827/* Transmit callback funtion. */
828static int mcs_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
829{
830 unsigned long flags;
831 struct mcs_cb *mcs;
832 int wraplen;
833 int ret = 0;
834
835
836 if (skb == NULL || ndev == NULL)
837 return -EINVAL;
838
839 netif_stop_queue(ndev);
840 mcs = netdev_priv(ndev);
841
842 spin_lock_irqsave(&mcs->lock, flags);
843
844 mcs->new_speed = irda_get_next_speed(skb);
845 if (likely(mcs->new_speed == mcs->speed))
846 mcs->new_speed = 0;
847
848 /* SIR speed */
849 if(mcs->speed < 576000) {
850 wraplen = mcs_wrap_sir_skb(skb, mcs->out_buf);
851 }
852 /* MIR speed */
853 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
854 wraplen = mcs_wrap_mir_skb(skb, mcs->out_buf);
855 }
856 /* FIR speed */
857 else {
858 wraplen = mcs_wrap_fir_skb(skb, mcs->out_buf);
859 }
860 usb_fill_bulk_urb(mcs->tx_urb, mcs->usbdev,
861 usb_sndbulkpipe(mcs->usbdev, mcs->ep_out),
862 mcs->out_buf, wraplen, mcs_send_irq, mcs);
863
864 if ((ret = usb_submit_urb(mcs->tx_urb, GFP_ATOMIC))) {
865 IRDA_ERROR("failed tx_urb: %d", ret);
866 switch (ret) {
867 case -ENODEV:
868 case -EPIPE:
869 break;
870 default:
871 mcs->stats.tx_errors++;
872 netif_start_queue(ndev);
873 }
874 } else {
875 mcs->stats.tx_packets++;
876 mcs->stats.tx_bytes += skb->len;
877 }
878
879 dev_kfree_skb(skb);
880 spin_unlock_irqrestore(&mcs->lock, flags);
881 return ret;
882}
883
884/*
885 * This function is called by the USB subsystem for each new device in the
886 * system. Need to verify the device and if it is, then start handling it.
887 */
888static int mcs_probe(struct usb_interface *intf,
889 const struct usb_device_id *id)
890{
891 struct usb_device *udev = interface_to_usbdev(intf);
892 struct net_device *ndev = NULL;
893 struct mcs_cb *mcs;
894 int ret = -ENOMEM;
895
896 ndev = alloc_irdadev(sizeof(*mcs));
897 if (!ndev)
898 goto error1;
899
900 IRDA_DEBUG(1, "MCS7780 USB-IrDA bridge found at %d.", udev->devnum);
901
902 /* what is it realy for? */
903 SET_MODULE_OWNER(ndev);
904 SET_NETDEV_DEV(ndev, &intf->dev);
905
906 ret = usb_reset_configuration(udev);
907 if (ret != 0) {
908 IRDA_ERROR("mcs7780: usb reset configuration failed");
909 goto error2;
910 }
911
912 mcs = netdev_priv(ndev);
913 mcs->usbdev = udev;
914 mcs->netdev = ndev;
915 spin_lock_init(&mcs->lock);
916
917 /* Initialize QoS for this device */
918 irda_init_max_qos_capabilies(&mcs->qos);
919
920 /* That's the Rx capability. */
921 mcs->qos.baud_rate.bits &=
922 IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200
923 | IR_576000 | IR_1152000 | (IR_4000000 << 8);
924
925
926 mcs->qos.min_turn_time.bits &= qos_mtt_bits;
927 irda_qos_bits_to_value(&mcs->qos);
928
929 /* Speed change work initialisation*/
David Howellsc4028952006-11-22 14:57:56 +0000930 INIT_WORK(&mcs->work, mcs_speed_work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700931
932 /* Override the network functions we need to use */
933 ndev->hard_start_xmit = mcs_hard_xmit;
934 ndev->open = mcs_net_open;
935 ndev->stop = mcs_net_close;
936 ndev->get_stats = mcs_net_get_stats;
937 ndev->do_ioctl = mcs_net_ioctl;
938
939 if (!intf->cur_altsetting)
940 goto error2;
941
942 ret = mcs_find_endpoints(mcs, intf->cur_altsetting->endpoint,
943 intf->cur_altsetting->desc.bNumEndpoints);
944 if (!ret) {
945 ret = -ENODEV;
946 goto error2;
947 }
948
949 ret = register_netdev(ndev);
950 if (ret != 0)
951 goto error2;
952
953 IRDA_DEBUG(1, "IrDA: Registered MosChip MCS7780 device as %s",
954 ndev->name);
955
956 mcs->transceiver_type = transceiver_type;
957 mcs->sir_tweak = sir_tweak;
958 mcs->receive_mode = receive_mode;
959
960 usb_set_intfdata(intf, mcs);
961 return 0;
962
963 error2:
964 free_netdev(ndev);
965
966 error1:
967 return ret;
968}
969
970/* The current device is removed, the USB layer tells us to shut down. */
971static void mcs_disconnect(struct usb_interface *intf)
972{
973 struct mcs_cb *mcs = usb_get_intfdata(intf);
974
975 if (!mcs)
976 return;
977
978 flush_scheduled_work();
979
980 unregister_netdev(mcs->netdev);
981 free_netdev(mcs->netdev);
982
983 usb_set_intfdata(intf, NULL);
984 IRDA_DEBUG(0, "MCS7780 now disconnected.");
985}
986
987/* Module insertion */
988static int __init mcs_init(void)
989{
990 int result;
991
992 /* register this driver with the USB subsystem */
993 result = usb_register(&mcs_driver);
994 if (result)
995 IRDA_ERROR("usb_register failed. Error number %d", result);
996
997 return result;
998}
999module_init(mcs_init);
1000
1001/* Module removal */
1002static void __exit mcs_exit(void)
1003{
1004 /* deregister this driver with the USB subsystem */
1005 usb_deregister(&mcs_driver);
1006}
1007module_exit(mcs_exit);
1008