Merge git://git.infradead.org/~dwmw2/iommu-2.6.32
[cascardo/linux.git] / drivers / net / irda / mcs7780.c
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
70 static 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
76 MODULE_AUTHOR("Brian Pugh <bpugh@cs.pdx.edu>");
77 MODULE_DESCRIPTION("IrDA-USB Dongle Driver for MosChip MCS7780");
78 MODULE_VERSION("0.3alpha");
79 MODULE_LICENSE("GPL");
80
81 MODULE_DEVICE_TABLE(usb, mcs_table);
82
83 static int qos_mtt_bits = 0x07 /* > 1ms */ ;
84 module_param(qos_mtt_bits, int, 0);
85 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
86
87 static int receive_mode = 0x1;
88 module_param(receive_mode, int, 0);
89 MODULE_PARM_DESC(receive_mode,
90                  "Receive mode of the device (1:fast, 0:slow, default:1)");
91
92 static int sir_tweak = 1;
93 module_param(sir_tweak, int, 0444);
94 MODULE_PARM_DESC(sir_tweak,
95                  "Default pulse width (1:1.6us, 0:3/16 bit, default:1).");
96
97 static int transceiver_type = MCS_TSC_VISHAY;
98 module_param(transceiver_type, int, 0444);
99 MODULE_PARM_DESC(transceiver_type, "IR transceiver type, see mcs7780.h.");
100
101 static 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.
109 addr = (speed >> 8) & 0x0f
110
111 0x1   57600      0x2  115200     0x4 1152000     0x5    9600
112 0x6   38400      0x9    2400     0xa  576000     0xb   19200
113
114 4Mbps (or 2400) must be checked separately. Since it also has
115 to be programmed in a different manner that is not a big problem.
116 */
117 static __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. */
133 static 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. */
142 static 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 */
159 static 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. */
199 static 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. */
206 static 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 */
213 static 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
318 error:
319         IRDA_ERROR("%s\n", msg);
320         return ret;
321 }
322
323 /* Wraps the data in format for SIR */
324 static 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 */
339 static 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 */
365 static 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  */
391 static 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         return;
439 }
440
441 /* Unwrap received packets at FIR speed.  A 32 bit crc_ccitt checksum is
442  * used for the fcs.  Hands the unwrapped data off to the IrDA
443  * layer via a sk_buff.
444  */
445 static void mcs_unwrap_fir(struct mcs_cb *mcs, __u8 *buf, int len)
446 {
447         __u32 fcs;
448         int new_len;
449         struct sk_buff *skb;
450
451         /* Assume that the frames are going to fill a single packet
452          * rather than span multiple packets.  This is most likely a false
453          * assumption.
454          */
455
456         new_len = len - 4;
457         if(unlikely(new_len <= 0)) {
458                 IRDA_ERROR("%s short frame length %d\n",
459                            mcs->netdev->name, new_len);
460                 ++mcs->netdev->stats.rx_errors;
461                 ++mcs->netdev->stats.rx_length_errors;
462                 return;
463         }
464
465         fcs = ~(crc32_le(~0, buf, new_len));
466         if(fcs != get_unaligned_le32(buf + new_len)) {
467                 IRDA_ERROR("crc error calc 0x%x len %d\n", fcs, new_len);
468                 mcs->netdev->stats.rx_errors++;
469                 mcs->netdev->stats.rx_crc_errors++;
470                 return;
471         }
472
473         skb = dev_alloc_skb(new_len + 1);
474         if(unlikely(!skb)) {
475                 ++mcs->netdev->stats.rx_dropped;
476                 return;
477         }
478
479         skb_reserve(skb, 1);
480         skb_copy_to_linear_data(skb, buf, new_len);
481         skb_put(skb, new_len);
482         skb_reset_mac_header(skb);
483         skb->protocol = htons(ETH_P_IRDA);
484         skb->dev = mcs->netdev;
485
486         netif_rx(skb);
487
488         mcs->netdev->stats.rx_packets++;
489         mcs->netdev->stats.rx_bytes += new_len;
490
491         return;
492 }
493
494
495 /* Allocates urbs for both receive and transmit.
496  * If alloc fails return error code 0 (fail) otherwise
497  * return error code 1 (success).
498  */
499 static inline int mcs_setup_urbs(struct mcs_cb *mcs)
500 {
501         mcs->rx_urb = NULL;
502
503         mcs->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
504         if (!mcs->tx_urb)
505                 return 0;
506
507         mcs->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
508         if (!mcs->rx_urb)
509                 return 0;
510
511         return 1;
512 }
513
514 /* Sets up state to be initially outside frame, gets receive urb,
515  * sets status to successful and then submits the urb to start
516  * receiving the data.
517  */
518 static inline int mcs_receive_start(struct mcs_cb *mcs)
519 {
520         mcs->rx_buff.in_frame = FALSE;
521         mcs->rx_buff.state = OUTSIDE_FRAME;
522
523         usb_fill_bulk_urb(mcs->rx_urb, mcs->usbdev,
524                           usb_rcvbulkpipe(mcs->usbdev, mcs->ep_in),
525                           mcs->in_buf, 4096, mcs_receive_irq, mcs);
526
527         mcs->rx_urb->status = 0;
528         return usb_submit_urb(mcs->rx_urb, GFP_KERNEL);
529 }
530
531 /* Finds the in and out endpoints for the mcs control block */
532 static inline int mcs_find_endpoints(struct mcs_cb *mcs,
533                                      struct usb_host_endpoint *ep, int epnum)
534 {
535         int i;
536         int ret = 0;
537
538         /* If no place to store the endpoints just return */
539         if (!ep)
540                 return ret;
541
542         /* cycle through all endpoints, find the first two that are DIR_IN */
543         for (i = 0; i < epnum; i++) {
544                 if (ep[i].desc.bEndpointAddress & USB_DIR_IN)
545                         mcs->ep_in = ep[i].desc.bEndpointAddress;
546                 else
547                         mcs->ep_out = ep[i].desc.bEndpointAddress;
548
549                 /* MosChip says that the chip has only two bulk
550                  * endpoints. Find one for each direction and move on.
551                  */
552                 if ((mcs->ep_in != 0) && (mcs->ep_out != 0)) {
553                         ret = 1;
554                         break;
555                 }
556         }
557
558         return ret;
559 }
560
561 static void mcs_speed_work(struct work_struct *work)
562 {
563         struct mcs_cb *mcs = container_of(work, struct mcs_cb, work);
564         struct net_device *netdev = mcs->netdev;
565
566         mcs_speed_change(mcs);
567         netif_wake_queue(netdev);
568 }
569
570 /* Function to change the speed of the mcs7780.  Fully supports SIR,
571  * MIR, and FIR speeds.
572  */
573 static int mcs_speed_change(struct mcs_cb *mcs)
574 {
575         int ret = 0;
576         int rst = 0;
577         int cnt = 0;
578         __u16 nspeed;
579         __u16 rval;
580
581         nspeed = mcs_speed_set[(mcs->new_speed >> 8) & 0x0f];
582
583         do {
584                 mcs_get_reg(mcs, MCS_RESV_REG, &rval);
585         } while(cnt++ < 100 && (rval & MCS_IRINTX));
586
587         if (cnt > 100) {
588                 IRDA_ERROR("unable to change speed\n");
589                 ret = -EIO;
590                 goto error;
591         }
592
593         mcs_get_reg(mcs, MCS_MODE_REG, &rval);
594
595         /* MINRXPW values recomended by MosChip */
596         if (mcs->new_speed <= 115200) {
597                 rval &= ~MCS_FIR;
598
599                 if ((rst = (mcs->speed > 115200)))
600                         mcs_set_reg(mcs, MCS_MINRXPW_REG, 0);
601
602         } else if (mcs->new_speed <= 1152000) {
603                 rval &= ~MCS_FIR;
604
605                 if ((rst = !(mcs->speed == 576000 || mcs->speed == 1152000)))
606                         mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
607
608         } else {
609                 rval |= MCS_FIR;
610
611                 if ((rst = (mcs->speed != 4000000)))
612                         mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
613
614         }
615
616         rval &= ~MCS_SPEED_MASK;
617         rval |= nspeed;
618
619         ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
620         if (unlikely(ret))
621                 goto error;
622
623         if (rst)
624                 switch (mcs->transceiver_type) {
625                 case MCS_TSC_VISHAY:
626                         ret = mcs_setup_transceiver_vishay(mcs);
627                         break;
628
629                 case MCS_TSC_SHARP:
630                         ret = mcs_setup_transceiver_sharp(mcs);
631                         break;
632
633                 case MCS_TSC_AGILENT:
634                         ret = mcs_setup_transceiver_agilent(mcs);
635                         break;
636
637                 default:
638                         ret = 1;
639                         IRDA_WARNING("Unknown transceiver type: %d\n",
640                                      mcs->transceiver_type);
641                 }
642         if (unlikely(ret))
643                 goto error;
644
645         mcs_get_reg(mcs, MCS_MODE_REG, &rval);
646         rval &= ~MCS_RESET;
647         ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
648
649         mcs->speed = mcs->new_speed;
650         error:
651                 mcs->new_speed = 0;
652                 return ret;
653 }
654
655 /* Ioctl calls not supported at this time.  Can be an area of future work. */
656 static int mcs_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
657 {
658         /* struct if_irda_req *irq = (struct if_irda_req *)rq; */
659         /* struct mcs_cb *mcs = netdev_priv(netdev); */
660         int ret = 0;
661
662         switch (cmd) {
663         default:
664                 ret = -EOPNOTSUPP;
665         }
666
667         return ret;
668 }
669
670 /* Network device is taken down, done by "ifconfig irda0 down" */
671 static int mcs_net_close(struct net_device *netdev)
672 {
673         int ret = 0;
674         struct mcs_cb *mcs = netdev_priv(netdev);
675
676         /* Stop transmit processing */
677         netif_stop_queue(netdev);
678
679         kfree_skb(mcs->rx_buff.skb);
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" */
696 static 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\n");
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 /* Receive callback function.  */
759 static void mcs_receive_irq(struct urb *urb)
760 {
761         __u8 *bytes;
762         struct mcs_cb *mcs = urb->context;
763         int i;
764         int ret;
765
766         if (!netif_running(mcs->netdev))
767                 return;
768
769         if (urb->status)
770                 return;
771
772         if (urb->actual_length > 0) {
773                 bytes = urb->transfer_buffer;
774
775                 /* MCS returns frames without BOF and EOF
776                  * I assume it returns whole frames.
777                  */
778                 /* SIR speed */
779                 if(mcs->speed < 576000) {
780                         async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
781                                   &mcs->rx_buff, 0xc0);
782
783                         for (i = 0; i < urb->actual_length; i++)
784                                 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
785                                           &mcs->rx_buff, bytes[i]);
786
787                         async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
788                                   &mcs->rx_buff, 0xc1);
789                 }
790                 /* MIR speed */
791                 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
792                         mcs_unwrap_mir(mcs, urb->transfer_buffer,
793                                 urb->actual_length);
794                 }
795                 /* FIR speed */
796                 else {
797                         mcs_unwrap_fir(mcs, urb->transfer_buffer,
798                                 urb->actual_length);
799                 }
800                 do_gettimeofday(&mcs->rx_time);
801         }
802
803         ret = usb_submit_urb(urb, GFP_ATOMIC);
804 }
805
806 /* Transmit callback funtion.  */
807 static 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 funtion.  */
819 static 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                 IRDA_ERROR("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
872 static 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  */
883 static 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         IRDA_DEBUG(1, "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                 IRDA_ERROR("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                 goto error2;
929
930         ret = mcs_find_endpoints(mcs, intf->cur_altsetting->endpoint,
931                                  intf->cur_altsetting->desc.bNumEndpoints);
932         if (!ret) {
933                 ret = -ENODEV;
934                 goto error2;
935         }
936
937         ret = register_netdev(ndev);
938         if (ret != 0)
939                 goto error2;
940
941         IRDA_DEBUG(1, "IrDA: Registered MosChip MCS7780 device as %s\n",
942                    ndev->name);
943
944         mcs->transceiver_type = transceiver_type;
945         mcs->sir_tweak = sir_tweak;
946         mcs->receive_mode = receive_mode;
947
948         usb_set_intfdata(intf, mcs);
949         return 0;
950
951         error2:
952                 free_netdev(ndev);
953
954         error1:
955                 return ret;
956 }
957
958 /* The current device is removed, the USB layer tells us to shut down. */
959 static void mcs_disconnect(struct usb_interface *intf)
960 {
961         struct mcs_cb *mcs = usb_get_intfdata(intf);
962
963         if (!mcs)
964                 return;
965
966         flush_scheduled_work();
967
968         unregister_netdev(mcs->netdev);
969         free_netdev(mcs->netdev);
970
971         usb_set_intfdata(intf, NULL);
972         IRDA_DEBUG(0, "MCS7780 now disconnected.\n");
973 }
974
975 /* Module insertion */
976 static int __init mcs_init(void)
977 {
978         int result;
979
980         /* register this driver with the USB subsystem */
981         result = usb_register(&mcs_driver);
982         if (result)
983                 IRDA_ERROR("usb_register failed. Error number %d\n", result);
984
985         return result;
986 }
987 module_init(mcs_init);
988
989 /* Module removal */
990 static void __exit mcs_exit(void)
991 {
992         /* deregister this driver with the USB subsystem */
993         usb_deregister(&mcs_driver);
994 }
995 module_exit(mcs_exit);
996