blob: 16f8ffb50e0456aeb52c354dca9deeda8c20a041 [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>
Samuel Ortizc6ae5222006-05-25 16:19:22 -070051#include <linux/slab.h>
Samuel Ortizc6ae5222006-05-25 16:19:22 -070052#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
Adrian Bunk7263ade2006-06-29 12:39:07 -0700100static struct usb_driver mcs_driver = {
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700101 .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;
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400193error:
194 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700195}
196
197/* Setup a communication between mcs7780 and agilent chip. */
198static inline int mcs_setup_transceiver_agilent(struct mcs_cb *mcs)
199{
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700200 IRDA_WARNING("This transceiver type is not supported yet.\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700201 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{
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700207 IRDA_WARNING("This transceiver type is not supported yet.\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700208 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 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:
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700279 IRDA_WARNING("Unknown transceiver type: %d\n",
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700280 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:
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700318 IRDA_ERROR("%s\n", msg);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700319 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. */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300353 skb_copy_from_linear_data(skb, buf + 2, skb->len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700354 /* 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 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300377 skb_copy_from_linear_data(skb, buf + 2, skb->len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700378 /* 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 IRDA_ERROR("%s short frame length %d\n",
403 mcs->netdev->name, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800404 ++mcs->netdev->stats.rx_errors;
405 ++mcs->netdev->stats.rx_length_errors;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700406 return;
407 }
408 fcs = 0;
409 fcs = irda_calc_crc16(~fcs, buf, len);
410
411 if(fcs != GOOD_FCS) {
412 IRDA_ERROR("crc error calc 0x%x len %d\n",
413 fcs, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800414 mcs->netdev->stats.rx_errors++;
415 mcs->netdev->stats.rx_crc_errors++;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700416 return;
417 }
418
419 skb = dev_alloc_skb(new_len + 1);
420 if(unlikely(!skb)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800421 ++mcs->netdev->stats.rx_dropped;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700422 return;
423 }
424
425 skb_reserve(skb, 1);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300426 skb_copy_to_linear_data(skb, buf, new_len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700427 skb_put(skb, new_len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700428 skb_reset_mac_header(skb);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700429 skb->protocol = htons(ETH_P_IRDA);
430 skb->dev = mcs->netdev;
431
432 netif_rx(skb);
433
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800434 mcs->netdev->stats.rx_packets++;
435 mcs->netdev->stats.rx_bytes += new_len;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700436}
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 IRDA_ERROR("%s short frame length %d\n",
456 mcs->netdev->name, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800457 ++mcs->netdev->stats.rx_errors;
458 ++mcs->netdev->stats.rx_length_errors;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700459 return;
460 }
461
462 fcs = ~(crc32_le(~0, buf, new_len));
Harvey Harrison6caf52a42008-04-29 01:03:36 -0700463 if(fcs != get_unaligned_le32(buf + new_len)) {
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700464 IRDA_ERROR("crc error calc 0x%x len %d\n", fcs, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800465 mcs->netdev->stats.rx_errors++;
466 mcs->netdev->stats.rx_crc_errors++;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700467 return;
468 }
469
470 skb = dev_alloc_skb(new_len + 1);
471 if(unlikely(!skb)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800472 ++mcs->netdev->stats.rx_dropped;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700473 return;
474 }
475
476 skb_reserve(skb, 1);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300477 skb_copy_to_linear_data(skb, buf, new_len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700478 skb_put(skb, new_len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700479 skb_reset_mac_header(skb);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700480 skb->protocol = htons(ETH_P_IRDA);
481 skb->dev = mcs->netdev;
482
483 netif_rx(skb);
484
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800485 mcs->netdev->stats.rx_packets++;
486 mcs->netdev->stats.rx_bytes += new_len;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700487}
488
489
490/* Allocates urbs for both receive and transmit.
491 * If alloc fails return error code 0 (fail) otherwise
492 * return error code 1 (success).
493 */
494static inline int mcs_setup_urbs(struct mcs_cb *mcs)
495{
496 mcs->rx_urb = NULL;
497
498 mcs->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
499 if (!mcs->tx_urb)
500 return 0;
501
502 mcs->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400503 if (!mcs->rx_urb) {
504 usb_free_urb(mcs->tx_urb);
505 mcs->tx_urb = NULL;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700506 return 0;
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400507 }
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700508
509 return 1;
510}
511
512/* Sets up state to be initially outside frame, gets receive urb,
513 * sets status to successful and then submits the urb to start
514 * receiving the data.
515 */
516static inline int mcs_receive_start(struct mcs_cb *mcs)
517{
518 mcs->rx_buff.in_frame = FALSE;
519 mcs->rx_buff.state = OUTSIDE_FRAME;
520
521 usb_fill_bulk_urb(mcs->rx_urb, mcs->usbdev,
522 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_in),
523 mcs->in_buf, 4096, mcs_receive_irq, mcs);
524
525 mcs->rx_urb->status = 0;
526 return usb_submit_urb(mcs->rx_urb, GFP_KERNEL);
527}
528
529/* Finds the in and out endpoints for the mcs control block */
530static inline int mcs_find_endpoints(struct mcs_cb *mcs,
531 struct usb_host_endpoint *ep, int epnum)
532{
533 int i;
534 int ret = 0;
535
536 /* If no place to store the endpoints just return */
537 if (!ep)
538 return ret;
539
540 /* cycle through all endpoints, find the first two that are DIR_IN */
541 for (i = 0; i < epnum; i++) {
542 if (ep[i].desc.bEndpointAddress & USB_DIR_IN)
543 mcs->ep_in = ep[i].desc.bEndpointAddress;
544 else
545 mcs->ep_out = ep[i].desc.bEndpointAddress;
546
547 /* MosChip says that the chip has only two bulk
548 * endpoints. Find one for each direction and move on.
549 */
550 if ((mcs->ep_in != 0) && (mcs->ep_out != 0)) {
551 ret = 1;
552 break;
553 }
554 }
555
556 return ret;
557}
558
David Howellsc4028952006-11-22 14:57:56 +0000559static void mcs_speed_work(struct work_struct *work)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700560{
David Howellsc4028952006-11-22 14:57:56 +0000561 struct mcs_cb *mcs = container_of(work, struct mcs_cb, work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700562 struct net_device *netdev = mcs->netdev;
563
564 mcs_speed_change(mcs);
565 netif_wake_queue(netdev);
566}
567
568/* Function to change the speed of the mcs7780. Fully supports SIR,
569 * MIR, and FIR speeds.
570 */
571static int mcs_speed_change(struct mcs_cb *mcs)
572{
573 int ret = 0;
574 int rst = 0;
575 int cnt = 0;
576 __u16 nspeed;
577 __u16 rval;
578
579 nspeed = mcs_speed_set[(mcs->new_speed >> 8) & 0x0f];
580
581 do {
582 mcs_get_reg(mcs, MCS_RESV_REG, &rval);
583 } while(cnt++ < 100 && (rval & MCS_IRINTX));
584
Roel Kluinfb088672009-02-12 16:42:31 -0800585 if (cnt > 100) {
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700586 IRDA_ERROR("unable to change speed\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700587 ret = -EIO;
588 goto error;
589 }
590
591 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
592
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300593 /* MINRXPW values recommended by MosChip */
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700594 if (mcs->new_speed <= 115200) {
595 rval &= ~MCS_FIR;
596
597 if ((rst = (mcs->speed > 115200)))
598 mcs_set_reg(mcs, MCS_MINRXPW_REG, 0);
599
600 } else if (mcs->new_speed <= 1152000) {
601 rval &= ~MCS_FIR;
602
603 if ((rst = !(mcs->speed == 576000 || mcs->speed == 1152000)))
604 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
605
606 } else {
607 rval |= MCS_FIR;
608
609 if ((rst = (mcs->speed != 4000000)))
610 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
611
612 }
613
614 rval &= ~MCS_SPEED_MASK;
615 rval |= nspeed;
616
617 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
618 if (unlikely(ret))
619 goto error;
620
621 if (rst)
622 switch (mcs->transceiver_type) {
623 case MCS_TSC_VISHAY:
624 ret = mcs_setup_transceiver_vishay(mcs);
625 break;
626
627 case MCS_TSC_SHARP:
628 ret = mcs_setup_transceiver_sharp(mcs);
629 break;
630
631 case MCS_TSC_AGILENT:
632 ret = mcs_setup_transceiver_agilent(mcs);
633 break;
634
635 default:
636 ret = 1;
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700637 IRDA_WARNING("Unknown transceiver type: %d\n",
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700638 mcs->transceiver_type);
639 }
640 if (unlikely(ret))
641 goto error;
642
643 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
644 rval &= ~MCS_RESET;
645 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
646
647 mcs->speed = mcs->new_speed;
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400648error:
649 mcs->new_speed = 0;
650 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700651}
652
653/* Ioctl calls not supported at this time. Can be an area of future work. */
654static int mcs_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
655{
656 /* struct if_irda_req *irq = (struct if_irda_req *)rq; */
657 /* struct mcs_cb *mcs = netdev_priv(netdev); */
658 int ret = 0;
659
660 switch (cmd) {
661 default:
662 ret = -EOPNOTSUPP;
663 }
664
665 return ret;
666}
667
668/* Network device is taken down, done by "ifconfig irda0 down" */
669static int mcs_net_close(struct net_device *netdev)
670{
671 int ret = 0;
672 struct mcs_cb *mcs = netdev_priv(netdev);
673
674 /* Stop transmit processing */
675 netif_stop_queue(netdev);
676
Hinko Kocevar0ff80432007-12-16 14:08:22 -0800677 kfree_skb(mcs->rx_buff.skb);
678
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700679 /* kill and free the receive and transmit URBs */
680 usb_kill_urb(mcs->rx_urb);
681 usb_free_urb(mcs->rx_urb);
682 usb_kill_urb(mcs->tx_urb);
683 usb_free_urb(mcs->tx_urb);
684
685 /* Stop and remove instance of IrLAP */
686 if (mcs->irlap)
687 irlap_close(mcs->irlap);
688
689 mcs->irlap = NULL;
690 return ret;
691}
692
693/* Network device is taken up, done by "ifconfig irda0 up" */
694static int mcs_net_open(struct net_device *netdev)
695{
696 struct mcs_cb *mcs = netdev_priv(netdev);
697 char hwname[16];
698 int ret = 0;
699
700 ret = usb_clear_halt(mcs->usbdev,
701 usb_sndbulkpipe(mcs->usbdev, mcs->ep_in));
702 if (ret)
703 goto error1;
704 ret = usb_clear_halt(mcs->usbdev,
705 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_out));
706 if (ret)
707 goto error1;
708
709 ret = mcs_setup_transceiver(mcs);
710 if (ret)
711 goto error1;
712
713 ret = -ENOMEM;
714
715 /* Initialize for SIR/FIR to copy data directly into skb. */
716 mcs->receiving = 0;
717 mcs->rx_buff.truesize = IRDA_SKB_MAX_MTU;
718 mcs->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
719 if (!mcs->rx_buff.skb)
720 goto error1;
721
722 skb_reserve(mcs->rx_buff.skb, 1);
723 mcs->rx_buff.head = mcs->rx_buff.skb->data;
724 do_gettimeofday(&mcs->rx_time);
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) {
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700734 IRDA_ERROR("mcs7780: irlap_open failed\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700735 goto error2;
736 }
737
738 if (!mcs_setup_urbs(mcs))
Julia Lawall9b0f1d82010-08-05 10:22:00 +0000739 goto error3;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700740
741 ret = mcs_receive_start(mcs);
742 if (ret)
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400743 goto error4;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700744
745 netif_start_queue(netdev);
746 return 0;
747
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400748error4:
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;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700757}
758
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700759/* Receive callback function. */
David Howells7d12e782006-10-05 14:55:46 +0100760static void mcs_receive_irq(struct urb *urb)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700761{
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) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800781 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700782 &mcs->rx_buff, 0xc0);
783
784 for (i = 0; i < urb->actual_length; i++)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800785 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700786 &mcs->rx_buff, bytes[i]);
787
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800788 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700789 &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 }
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700801 do_gettimeofday(&mcs->rx_time);
802 }
803
804 ret = usb_submit_urb(urb, GFP_ATOMIC);
805}
806
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300807/* Transmit callback function. */
David Howells7d12e782006-10-05 14:55:46 +0100808static void mcs_send_irq(struct urb *urb)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700809{
810 struct mcs_cb *mcs = urb->context;
811 struct net_device *ndev = mcs->netdev;
812
813 if (unlikely(mcs->new_speed))
814 schedule_work(&mcs->work);
815 else
816 netif_wake_queue(ndev);
817}
818
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300819/* Transmit callback function. */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000820static netdev_tx_t mcs_hard_xmit(struct sk_buff *skb,
821 struct net_device *ndev)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700822{
823 unsigned long flags;
824 struct mcs_cb *mcs;
825 int wraplen;
826 int ret = 0;
827
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700828 netif_stop_queue(ndev);
829 mcs = netdev_priv(ndev);
830
831 spin_lock_irqsave(&mcs->lock, flags);
832
833 mcs->new_speed = irda_get_next_speed(skb);
834 if (likely(mcs->new_speed == mcs->speed))
835 mcs->new_speed = 0;
836
837 /* SIR speed */
838 if(mcs->speed < 576000) {
839 wraplen = mcs_wrap_sir_skb(skb, mcs->out_buf);
840 }
841 /* MIR speed */
842 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
843 wraplen = mcs_wrap_mir_skb(skb, mcs->out_buf);
844 }
845 /* FIR speed */
846 else {
847 wraplen = mcs_wrap_fir_skb(skb, mcs->out_buf);
848 }
849 usb_fill_bulk_urb(mcs->tx_urb, mcs->usbdev,
850 usb_sndbulkpipe(mcs->usbdev, mcs->ep_out),
851 mcs->out_buf, wraplen, mcs_send_irq, mcs);
852
853 if ((ret = usb_submit_urb(mcs->tx_urb, GFP_ATOMIC))) {
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700854 IRDA_ERROR("failed tx_urb: %d\n", ret);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700855 switch (ret) {
856 case -ENODEV:
857 case -EPIPE:
858 break;
859 default:
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800860 mcs->netdev->stats.tx_errors++;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700861 netif_start_queue(ndev);
862 }
863 } else {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800864 mcs->netdev->stats.tx_packets++;
865 mcs->netdev->stats.tx_bytes += skb->len;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700866 }
867
868 dev_kfree_skb(skb);
869 spin_unlock_irqrestore(&mcs->lock, flags);
Patrick McHardy4bd73ae2009-06-12 03:17:19 +0000870 return NETDEV_TX_OK;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700871}
872
Stephen Hemmingerddc2a922009-03-20 19:35:36 +0000873static const struct net_device_ops mcs_netdev_ops = {
874 .ndo_open = mcs_net_open,
875 .ndo_stop = mcs_net_close,
876 .ndo_start_xmit = mcs_hard_xmit,
877 .ndo_do_ioctl = mcs_net_ioctl,
878};
879
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700880/*
881 * This function is called by the USB subsystem for each new device in the
882 * system. Need to verify the device and if it is, then start handling it.
883 */
884static int mcs_probe(struct usb_interface *intf,
885 const struct usb_device_id *id)
886{
887 struct usb_device *udev = interface_to_usbdev(intf);
888 struct net_device *ndev = NULL;
889 struct mcs_cb *mcs;
890 int ret = -ENOMEM;
891
892 ndev = alloc_irdadev(sizeof(*mcs));
893 if (!ndev)
894 goto error1;
895
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700896 IRDA_DEBUG(1, "MCS7780 USB-IrDA bridge found at %d.\n", udev->devnum);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700897
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700898 SET_NETDEV_DEV(ndev, &intf->dev);
899
900 ret = usb_reset_configuration(udev);
901 if (ret != 0) {
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700902 IRDA_ERROR("mcs7780: usb reset configuration failed\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700903 goto error2;
904 }
905
906 mcs = netdev_priv(ndev);
907 mcs->usbdev = udev;
908 mcs->netdev = ndev;
909 spin_lock_init(&mcs->lock);
910
911 /* Initialize QoS for this device */
912 irda_init_max_qos_capabilies(&mcs->qos);
913
914 /* That's the Rx capability. */
915 mcs->qos.baud_rate.bits &=
916 IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200
917 | IR_576000 | IR_1152000 | (IR_4000000 << 8);
918
919
920 mcs->qos.min_turn_time.bits &= qos_mtt_bits;
921 irda_qos_bits_to_value(&mcs->qos);
922
923 /* Speed change work initialisation*/
David Howellsc4028952006-11-22 14:57:56 +0000924 INIT_WORK(&mcs->work, mcs_speed_work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700925
Stephen Hemmingerddc2a922009-03-20 19:35:36 +0000926 ndev->netdev_ops = &mcs_netdev_ops;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700927
Peter Senna Tschudin67340112012-10-05 11:33:04 +0000928 if (!intf->cur_altsetting) {
929 ret = -ENOMEM;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700930 goto error2;
Peter Senna Tschudin67340112012-10-05 11:33:04 +0000931 }
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700932
933 ret = mcs_find_endpoints(mcs, intf->cur_altsetting->endpoint,
934 intf->cur_altsetting->desc.bNumEndpoints);
935 if (!ret) {
936 ret = -ENODEV;
937 goto error2;
938 }
939
940 ret = register_netdev(ndev);
941 if (ret != 0)
942 goto error2;
943
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700944 IRDA_DEBUG(1, "IrDA: Registered MosChip MCS7780 device as %s\n",
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700945 ndev->name);
946
947 mcs->transceiver_type = transceiver_type;
948 mcs->sir_tweak = sir_tweak;
949 mcs->receive_mode = receive_mode;
950
951 usb_set_intfdata(intf, mcs);
952 return 0;
953
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400954error2:
955 free_netdev(ndev);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700956
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400957error1:
958 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700959}
960
961/* The current device is removed, the USB layer tells us to shut down. */
962static void mcs_disconnect(struct usb_interface *intf)
963{
964 struct mcs_cb *mcs = usb_get_intfdata(intf);
965
966 if (!mcs)
967 return;
968
Tejun Heo23f333a2010-12-12 16:45:14 +0100969 cancel_work_sync(&mcs->work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700970
971 unregister_netdev(mcs->netdev);
972 free_netdev(mcs->netdev);
973
974 usb_set_intfdata(intf, NULL);
Samuel Ortiz599b1fa2007-04-20 22:12:07 -0700975 IRDA_DEBUG(0, "MCS7780 now disconnected.\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700976}
977
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800978module_usb_driver(mcs_driver);