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