blob: e1bb802d4a4d19957e49dc31724e0ccfb160ad5d [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;
Thomas Jarosch59e76ed2017-07-22 17:14:34 +0200144 void *dmabuf;
145 int ret;
146
147 dmabuf = kmalloc(sizeof(__u16), GFP_KERNEL);
148 if (!dmabuf)
149 return -ENOMEM;
150
151 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
152 MCS_RD_RTYPE, 0, reg, dmabuf, 2,
153 msecs_to_jiffies(MCS_CTRL_TIMEOUT));
154
155 memcpy(val, dmabuf, sizeof(__u16));
156 kfree(dmabuf);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700157
158 return ret;
159}
160
161/* Setup a communication between mcs7780 and TFDU chips. It is described
162 * in more detail in the data sheet. The setup sequence puts the the
163 * vishay tranceiver into high speed mode. It will also receive SIR speed
164 * packets but at reduced sensitivity.
165 */
166
167/* 0: OK 1:ERROR */
168static inline int mcs_setup_transceiver_vishay(struct mcs_cb *mcs)
169{
170 int ret = 0;
171 __u16 rval;
172
173 /* mcs_get_reg should read exactly two bytes from the dongle */
174 ret = mcs_get_reg(mcs, MCS_XCVR_REG, &rval);
175 if (unlikely(ret != 2)) {
176 ret = -EIO;
177 goto error;
178 }
179
180 /* The MCS_XCVR_CONF bit puts the transceiver into configuration
181 * mode. The MCS_MODE0 bit must start out high (1) and then
182 * transition to low and the MCS_STFIR and MCS_MODE1 bits must
183 * be low.
184 */
185 rval |= (MCS_MODE0 | MCS_XCVR_CONF);
186 rval &= ~MCS_STFIR;
187 rval &= ~MCS_MODE1;
188 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
189 if (unlikely(ret))
190 goto error;
191
192 rval &= ~MCS_MODE0;
193 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
194 if (unlikely(ret))
195 goto error;
196
197 rval &= ~MCS_XCVR_CONF;
198 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
199 if (unlikely(ret))
200 goto error;
201
202 ret = 0;
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400203error:
204 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700205}
206
207/* Setup a communication between mcs7780 and agilent chip. */
208static inline int mcs_setup_transceiver_agilent(struct mcs_cb *mcs)
209{
Joe Perches6c910232014-11-11 13:37:30 -0800210 net_warn_ratelimited("This transceiver type is not supported yet\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700211 return 1;
212}
213
214/* Setup a communication between mcs7780 and sharp chip. */
215static inline int mcs_setup_transceiver_sharp(struct mcs_cb *mcs)
216{
Joe Perches6c910232014-11-11 13:37:30 -0800217 net_warn_ratelimited("This transceiver type is not supported yet\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700218 return 1;
219}
220
221/* Common setup for all transceivers */
222static inline int mcs_setup_transceiver(struct mcs_cb *mcs)
223{
224 int ret = 0;
225 __u16 rval;
Joe Perches6c910232014-11-11 13:37:30 -0800226 const char *msg;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700227
Joe Perches6c910232014-11-11 13:37:30 -0800228 msg = "Basic transceiver setup error";
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700229
230 /* read value of MODE Register, set the DRIVER and RESET bits
231 * and write value back out to MODE Register
232 */
233 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
234 if(unlikely(ret != 2))
235 goto error;
236 rval |= MCS_DRIVER; /* put the mcs7780 into configuration mode. */
237 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
238 if(unlikely(ret))
239 goto error;
240
241 rval = 0; /* set min pulse width to 0 initially. */
242 ret = mcs_set_reg(mcs, MCS_MINRXPW_REG, rval);
243 if(unlikely(ret))
244 goto error;
245
246 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
247 if(unlikely(ret != 2))
248 goto error;
249
250 rval &= ~MCS_FIR; /* turn off fir mode. */
251 if(mcs->sir_tweak)
252 rval |= MCS_SIR16US; /* 1.6us pulse width */
253 else
254 rval &= ~MCS_SIR16US; /* 3/16 bit time pulse width */
255
256 /* make sure ask mode and back to back packets are off. */
257 rval &= ~(MCS_BBTG | MCS_ASK);
258
259 rval &= ~MCS_SPEED_MASK;
260 rval |= MCS_SPEED_9600; /* make sure initial speed is 9600. */
261 mcs->speed = 9600;
262 mcs->new_speed = 0; /* new_speed is set to 0 */
263 rval &= ~MCS_PLLPWDN; /* disable power down. */
264
265 /* make sure device determines direction and that the auto send sip
266 * pulse are on.
267 */
268 rval |= MCS_DTD | MCS_SIPEN;
269
270 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
271 if(unlikely(ret))
272 goto error;
273
Joe Perches6c910232014-11-11 13:37:30 -0800274 msg = "transceiver model specific setup error";
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700275 switch (mcs->transceiver_type) {
276 case MCS_TSC_VISHAY:
277 ret = mcs_setup_transceiver_vishay(mcs);
278 break;
279
280 case MCS_TSC_SHARP:
281 ret = mcs_setup_transceiver_sharp(mcs);
282 break;
283
284 case MCS_TSC_AGILENT:
285 ret = mcs_setup_transceiver_agilent(mcs);
286 break;
287
288 default:
Joe Perches6c910232014-11-11 13:37:30 -0800289 net_warn_ratelimited("Unknown transceiver type: %d\n",
290 mcs->transceiver_type);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700291 ret = 1;
292 }
293 if (unlikely(ret))
294 goto error;
295
296 /* If transceiver is not SHARP, then if receive mode set
297 * on the RXFAST bit in the XCVR Register otherwise unset it
298 */
299 if (mcs->transceiver_type != MCS_TSC_SHARP) {
300
301 ret = mcs_get_reg(mcs, MCS_XCVR_REG, &rval);
302 if (unlikely(ret != 2))
303 goto error;
304 if (mcs->receive_mode)
305 rval |= MCS_RXFAST;
306 else
307 rval &= ~MCS_RXFAST;
308 ret = mcs_set_reg(mcs, MCS_XCVR_REG, rval);
309 if (unlikely(ret))
310 goto error;
311 }
312
Joe Perches6c910232014-11-11 13:37:30 -0800313 msg = "transceiver reset";
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700314
315 ret = mcs_get_reg(mcs, MCS_MODE_REG, &rval);
316 if (unlikely(ret != 2))
317 goto error;
318
319 /* reset the mcs7780 so all changes take effect. */
320 rval &= ~MCS_RESET;
321 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
322 if (unlikely(ret))
323 goto error;
324 else
325 return ret;
326
327error:
Joe Perches6c910232014-11-11 13:37:30 -0800328 net_err_ratelimited("%s\n", msg);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700329 return ret;
330}
331
332/* Wraps the data in format for SIR */
333static inline int mcs_wrap_sir_skb(struct sk_buff *skb, __u8 * buf)
334{
335 int wraplen;
336
337 /* 2: full frame length, including "the length" */
338 wraplen = async_wrap_skb(skb, buf + 2, 4094);
339
340 wraplen += 2;
341 buf[0] = wraplen & 0xff;
342 buf[1] = (wraplen >> 8) & 0xff;
343
344 return wraplen;
345}
346
347/* Wraps the data in format for FIR */
348static unsigned mcs_wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
349{
350 unsigned int len = 0;
351 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
352
353 /* add 2 bytes for length value and 4 bytes for fcs. */
354 len = skb->len + 6;
355
356 /* The mcs7780 requires that the first two bytes are the packet
357 * length in little endian order. Note: the length value includes
358 * the two bytes for the length value itself.
359 */
360 buf[0] = len & 0xff;
361 buf[1] = (len >> 8) & 0xff;
362 /* copy the data into the tx buffer. */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300363 skb_copy_from_linear_data(skb, buf + 2, skb->len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700364 /* put the fcs in the last four bytes in little endian order. */
365 buf[len - 4] = fcs & 0xff;
366 buf[len - 3] = (fcs >> 8) & 0xff;
367 buf[len - 2] = (fcs >> 16) & 0xff;
368 buf[len - 1] = (fcs >> 24) & 0xff;
369
370 return len;
371}
372
373/* Wraps the data in format for MIR */
374static unsigned mcs_wrap_mir_skb(const struct sk_buff *skb, __u8 *buf)
375{
376 __u16 fcs = 0;
377 int len = skb->len + 4;
378
379 fcs = ~(irda_calc_crc16(~fcs, skb->data, skb->len));
380 /* put the total packet length in first. Note: packet length
381 * value includes the two bytes that hold the packet length
382 * itself.
383 */
384 buf[0] = len & 0xff;
385 buf[1] = (len >> 8) & 0xff;
386 /* copy the data */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300387 skb_copy_from_linear_data(skb, buf + 2, skb->len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700388 /* put the fcs in last two bytes in little endian order. */
389 buf[len - 2] = fcs & 0xff;
390 buf[len - 1] = (fcs >> 8) & 0xff;
391
392 return len;
393}
394
395/* Unwrap received packets at MIR speed. A 16 bit crc_ccitt checksum is
396 * used for the fcs. When performed over the entire packet the result
397 * should be GOOD_FCS = 0xf0b8. Hands the unwrapped data off to the IrDA
398 * layer via a sk_buff.
399 */
400static void mcs_unwrap_mir(struct mcs_cb *mcs, __u8 *buf, int len)
401{
402 __u16 fcs;
403 int new_len;
404 struct sk_buff *skb;
405
406 /* Assume that the frames are going to fill a single packet
407 * rather than span multiple packets.
408 */
409
410 new_len = len - 2;
411 if(unlikely(new_len <= 0)) {
Joe Perches6c910232014-11-11 13:37:30 -0800412 net_err_ratelimited("%s short frame length %d\n",
413 mcs->netdev->name, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800414 ++mcs->netdev->stats.rx_errors;
415 ++mcs->netdev->stats.rx_length_errors;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700416 return;
417 }
418 fcs = 0;
419 fcs = irda_calc_crc16(~fcs, buf, len);
420
421 if(fcs != GOOD_FCS) {
Joe Perches6c910232014-11-11 13:37:30 -0800422 net_err_ratelimited("crc error calc 0x%x len %d\n",
423 fcs, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800424 mcs->netdev->stats.rx_errors++;
425 mcs->netdev->stats.rx_crc_errors++;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700426 return;
427 }
428
429 skb = dev_alloc_skb(new_len + 1);
430 if(unlikely(!skb)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800431 ++mcs->netdev->stats.rx_dropped;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700432 return;
433 }
434
435 skb_reserve(skb, 1);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300436 skb_copy_to_linear_data(skb, buf, new_len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700437 skb_put(skb, new_len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700438 skb_reset_mac_header(skb);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700439 skb->protocol = htons(ETH_P_IRDA);
440 skb->dev = mcs->netdev;
441
442 netif_rx(skb);
443
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800444 mcs->netdev->stats.rx_packets++;
445 mcs->netdev->stats.rx_bytes += new_len;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700446}
447
448/* Unwrap received packets at FIR speed. A 32 bit crc_ccitt checksum is
449 * used for the fcs. Hands the unwrapped data off to the IrDA
450 * layer via a sk_buff.
451 */
452static void mcs_unwrap_fir(struct mcs_cb *mcs, __u8 *buf, int len)
453{
454 __u32 fcs;
455 int new_len;
456 struct sk_buff *skb;
457
458 /* Assume that the frames are going to fill a single packet
459 * rather than span multiple packets. This is most likely a false
460 * assumption.
461 */
462
463 new_len = len - 4;
464 if(unlikely(new_len <= 0)) {
Joe Perches6c910232014-11-11 13:37:30 -0800465 net_err_ratelimited("%s short frame length %d\n",
466 mcs->netdev->name, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800467 ++mcs->netdev->stats.rx_errors;
468 ++mcs->netdev->stats.rx_length_errors;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700469 return;
470 }
471
472 fcs = ~(crc32_le(~0, buf, new_len));
Harvey Harrison6caf52a42008-04-29 01:03:36 -0700473 if(fcs != get_unaligned_le32(buf + new_len)) {
Joe Perches6c910232014-11-11 13:37:30 -0800474 net_err_ratelimited("crc error calc 0x%x len %d\n",
475 fcs, new_len);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800476 mcs->netdev->stats.rx_errors++;
477 mcs->netdev->stats.rx_crc_errors++;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700478 return;
479 }
480
481 skb = dev_alloc_skb(new_len + 1);
482 if(unlikely(!skb)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800483 ++mcs->netdev->stats.rx_dropped;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700484 return;
485 }
486
487 skb_reserve(skb, 1);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300488 skb_copy_to_linear_data(skb, buf, new_len);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700489 skb_put(skb, new_len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700490 skb_reset_mac_header(skb);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700491 skb->protocol = htons(ETH_P_IRDA);
492 skb->dev = mcs->netdev;
493
494 netif_rx(skb);
495
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800496 mcs->netdev->stats.rx_packets++;
497 mcs->netdev->stats.rx_bytes += new_len;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700498}
499
500
501/* Allocates urbs for both receive and transmit.
502 * If alloc fails return error code 0 (fail) otherwise
503 * return error code 1 (success).
504 */
505static inline int mcs_setup_urbs(struct mcs_cb *mcs)
506{
507 mcs->rx_urb = NULL;
508
509 mcs->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
510 if (!mcs->tx_urb)
511 return 0;
512
513 mcs->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400514 if (!mcs->rx_urb) {
515 usb_free_urb(mcs->tx_urb);
516 mcs->tx_urb = NULL;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700517 return 0;
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400518 }
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700519
520 return 1;
521}
522
523/* Sets up state to be initially outside frame, gets receive urb,
524 * sets status to successful and then submits the urb to start
525 * receiving the data.
526 */
527static inline int mcs_receive_start(struct mcs_cb *mcs)
528{
529 mcs->rx_buff.in_frame = FALSE;
530 mcs->rx_buff.state = OUTSIDE_FRAME;
531
532 usb_fill_bulk_urb(mcs->rx_urb, mcs->usbdev,
533 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_in),
534 mcs->in_buf, 4096, mcs_receive_irq, mcs);
535
536 mcs->rx_urb->status = 0;
537 return usb_submit_urb(mcs->rx_urb, GFP_KERNEL);
538}
539
540/* Finds the in and out endpoints for the mcs control block */
541static inline int mcs_find_endpoints(struct mcs_cb *mcs,
542 struct usb_host_endpoint *ep, int epnum)
543{
544 int i;
545 int ret = 0;
546
547 /* If no place to store the endpoints just return */
548 if (!ep)
549 return ret;
550
551 /* cycle through all endpoints, find the first two that are DIR_IN */
552 for (i = 0; i < epnum; i++) {
553 if (ep[i].desc.bEndpointAddress & USB_DIR_IN)
554 mcs->ep_in = ep[i].desc.bEndpointAddress;
555 else
556 mcs->ep_out = ep[i].desc.bEndpointAddress;
557
558 /* MosChip says that the chip has only two bulk
559 * endpoints. Find one for each direction and move on.
560 */
561 if ((mcs->ep_in != 0) && (mcs->ep_out != 0)) {
562 ret = 1;
563 break;
564 }
565 }
566
567 return ret;
568}
569
David Howellsc4028952006-11-22 14:57:56 +0000570static void mcs_speed_work(struct work_struct *work)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700571{
David Howellsc4028952006-11-22 14:57:56 +0000572 struct mcs_cb *mcs = container_of(work, struct mcs_cb, work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700573 struct net_device *netdev = mcs->netdev;
574
575 mcs_speed_change(mcs);
576 netif_wake_queue(netdev);
577}
578
579/* Function to change the speed of the mcs7780. Fully supports SIR,
580 * MIR, and FIR speeds.
581 */
582static int mcs_speed_change(struct mcs_cb *mcs)
583{
584 int ret = 0;
585 int rst = 0;
586 int cnt = 0;
587 __u16 nspeed;
588 __u16 rval;
589
590 nspeed = mcs_speed_set[(mcs->new_speed >> 8) & 0x0f];
591
592 do {
593 mcs_get_reg(mcs, MCS_RESV_REG, &rval);
594 } while(cnt++ < 100 && (rval & MCS_IRINTX));
595
Roel Kluinfb088672009-02-12 16:42:31 -0800596 if (cnt > 100) {
Joe Perches6c910232014-11-11 13:37:30 -0800597 net_err_ratelimited("unable to change speed\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700598 ret = -EIO;
599 goto error;
600 }
601
602 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
603
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 /* MINRXPW values recommended by MosChip */
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700605 if (mcs->new_speed <= 115200) {
606 rval &= ~MCS_FIR;
607
608 if ((rst = (mcs->speed > 115200)))
609 mcs_set_reg(mcs, MCS_MINRXPW_REG, 0);
610
611 } else if (mcs->new_speed <= 1152000) {
612 rval &= ~MCS_FIR;
613
614 if ((rst = !(mcs->speed == 576000 || mcs->speed == 1152000)))
615 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
616
617 } else {
618 rval |= MCS_FIR;
619
620 if ((rst = (mcs->speed != 4000000)))
621 mcs_set_reg(mcs, MCS_MINRXPW_REG, 5);
622
623 }
624
625 rval &= ~MCS_SPEED_MASK;
626 rval |= nspeed;
627
628 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
629 if (unlikely(ret))
630 goto error;
631
632 if (rst)
633 switch (mcs->transceiver_type) {
634 case MCS_TSC_VISHAY:
635 ret = mcs_setup_transceiver_vishay(mcs);
636 break;
637
638 case MCS_TSC_SHARP:
639 ret = mcs_setup_transceiver_sharp(mcs);
640 break;
641
642 case MCS_TSC_AGILENT:
643 ret = mcs_setup_transceiver_agilent(mcs);
644 break;
645
646 default:
647 ret = 1;
Joe Perches6c910232014-11-11 13:37:30 -0800648 net_warn_ratelimited("Unknown transceiver type: %d\n",
649 mcs->transceiver_type);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700650 }
651 if (unlikely(ret))
652 goto error;
653
654 mcs_get_reg(mcs, MCS_MODE_REG, &rval);
655 rval &= ~MCS_RESET;
656 ret = mcs_set_reg(mcs, MCS_MODE_REG, rval);
657
658 mcs->speed = mcs->new_speed;
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400659error:
660 mcs->new_speed = 0;
661 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700662}
663
664/* Ioctl calls not supported at this time. Can be an area of future work. */
665static int mcs_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
666{
667 /* struct if_irda_req *irq = (struct if_irda_req *)rq; */
668 /* struct mcs_cb *mcs = netdev_priv(netdev); */
669 int ret = 0;
670
671 switch (cmd) {
672 default:
673 ret = -EOPNOTSUPP;
674 }
675
676 return ret;
677}
678
679/* Network device is taken down, done by "ifconfig irda0 down" */
680static int mcs_net_close(struct net_device *netdev)
681{
682 int ret = 0;
683 struct mcs_cb *mcs = netdev_priv(netdev);
684
685 /* Stop transmit processing */
686 netif_stop_queue(netdev);
687
Hinko Kocevar0ff80432007-12-16 14:08:22 -0800688 kfree_skb(mcs->rx_buff.skb);
689
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700690 /* kill and free the receive and transmit URBs */
691 usb_kill_urb(mcs->rx_urb);
692 usb_free_urb(mcs->rx_urb);
693 usb_kill_urb(mcs->tx_urb);
694 usb_free_urb(mcs->tx_urb);
695
696 /* Stop and remove instance of IrLAP */
697 if (mcs->irlap)
698 irlap_close(mcs->irlap);
699
700 mcs->irlap = NULL;
701 return ret;
702}
703
704/* Network device is taken up, done by "ifconfig irda0 up" */
705static int mcs_net_open(struct net_device *netdev)
706{
707 struct mcs_cb *mcs = netdev_priv(netdev);
708 char hwname[16];
709 int ret = 0;
710
711 ret = usb_clear_halt(mcs->usbdev,
712 usb_sndbulkpipe(mcs->usbdev, mcs->ep_in));
713 if (ret)
714 goto error1;
715 ret = usb_clear_halt(mcs->usbdev,
716 usb_rcvbulkpipe(mcs->usbdev, mcs->ep_out));
717 if (ret)
718 goto error1;
719
720 ret = mcs_setup_transceiver(mcs);
721 if (ret)
722 goto error1;
723
724 ret = -ENOMEM;
725
726 /* Initialize for SIR/FIR to copy data directly into skb. */
727 mcs->receiving = 0;
728 mcs->rx_buff.truesize = IRDA_SKB_MAX_MTU;
729 mcs->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
730 if (!mcs->rx_buff.skb)
731 goto error1;
732
733 skb_reserve(mcs->rx_buff.skb, 1);
734 mcs->rx_buff.head = mcs->rx_buff.skb->data;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700735
736 /*
737 * Now that everything should be initialized properly,
738 * Open new IrLAP layer instance to take care of us...
739 * Note : will send immediately a speed change...
740 */
741 sprintf(hwname, "usb#%d", mcs->usbdev->devnum);
742 mcs->irlap = irlap_open(netdev, &mcs->qos, hwname);
743 if (!mcs->irlap) {
Joe Perches6c910232014-11-11 13:37:30 -0800744 net_err_ratelimited("mcs7780: irlap_open failed\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700745 goto error2;
746 }
747
748 if (!mcs_setup_urbs(mcs))
Julia Lawall9b0f1d82010-08-05 10:22:00 +0000749 goto error3;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700750
751 ret = mcs_receive_start(mcs);
752 if (ret)
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400753 goto error4;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700754
755 netif_start_queue(netdev);
756 return 0;
757
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400758error4:
759 usb_free_urb(mcs->rx_urb);
760 usb_free_urb(mcs->tx_urb);
761error3:
762 irlap_close(mcs->irlap);
763error2:
764 kfree_skb(mcs->rx_buff.skb);
765error1:
766 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700767}
768
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700769/* Receive callback function. */
David Howells7d12e782006-10-05 14:55:46 +0100770static void mcs_receive_irq(struct urb *urb)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700771{
772 __u8 *bytes;
773 struct mcs_cb *mcs = urb->context;
774 int i;
775 int ret;
776
777 if (!netif_running(mcs->netdev))
778 return;
779
780 if (urb->status)
781 return;
782
783 if (urb->actual_length > 0) {
784 bytes = urb->transfer_buffer;
785
786 /* MCS returns frames without BOF and EOF
787 * I assume it returns whole frames.
788 */
789 /* SIR speed */
790 if(mcs->speed < 576000) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800791 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700792 &mcs->rx_buff, 0xc0);
793
794 for (i = 0; i < urb->actual_length; i++)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800795 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700796 &mcs->rx_buff, bytes[i]);
797
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800798 async_unwrap_char(mcs->netdev, &mcs->netdev->stats,
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700799 &mcs->rx_buff, 0xc1);
800 }
801 /* MIR speed */
802 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
803 mcs_unwrap_mir(mcs, urb->transfer_buffer,
804 urb->actual_length);
805 }
806 /* FIR speed */
807 else {
808 mcs_unwrap_fir(mcs, urb->transfer_buffer,
809 urb->actual_length);
810 }
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700811 }
812
813 ret = usb_submit_urb(urb, GFP_ATOMIC);
814}
815
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300816/* Transmit callback function. */
David Howells7d12e782006-10-05 14:55:46 +0100817static void mcs_send_irq(struct urb *urb)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700818{
819 struct mcs_cb *mcs = urb->context;
820 struct net_device *ndev = mcs->netdev;
821
822 if (unlikely(mcs->new_speed))
823 schedule_work(&mcs->work);
824 else
825 netif_wake_queue(ndev);
826}
827
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300828/* Transmit callback function. */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000829static netdev_tx_t mcs_hard_xmit(struct sk_buff *skb,
830 struct net_device *ndev)
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700831{
832 unsigned long flags;
833 struct mcs_cb *mcs;
834 int wraplen;
835 int ret = 0;
836
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700837 netif_stop_queue(ndev);
838 mcs = netdev_priv(ndev);
839
840 spin_lock_irqsave(&mcs->lock, flags);
841
842 mcs->new_speed = irda_get_next_speed(skb);
843 if (likely(mcs->new_speed == mcs->speed))
844 mcs->new_speed = 0;
845
846 /* SIR speed */
847 if(mcs->speed < 576000) {
848 wraplen = mcs_wrap_sir_skb(skb, mcs->out_buf);
849 }
850 /* MIR speed */
851 else if(mcs->speed == 576000 || mcs->speed == 1152000) {
852 wraplen = mcs_wrap_mir_skb(skb, mcs->out_buf);
853 }
854 /* FIR speed */
855 else {
856 wraplen = mcs_wrap_fir_skb(skb, mcs->out_buf);
857 }
858 usb_fill_bulk_urb(mcs->tx_urb, mcs->usbdev,
859 usb_sndbulkpipe(mcs->usbdev, mcs->ep_out),
860 mcs->out_buf, wraplen, mcs_send_irq, mcs);
861
862 if ((ret = usb_submit_urb(mcs->tx_urb, GFP_ATOMIC))) {
Joe Perches6c910232014-11-11 13:37:30 -0800863 net_err_ratelimited("failed tx_urb: %d\n", ret);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700864 switch (ret) {
865 case -ENODEV:
866 case -EPIPE:
867 break;
868 default:
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800869 mcs->netdev->stats.tx_errors++;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700870 netif_start_queue(ndev);
871 }
872 } else {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800873 mcs->netdev->stats.tx_packets++;
874 mcs->netdev->stats.tx_bytes += skb->len;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700875 }
876
877 dev_kfree_skb(skb);
878 spin_unlock_irqrestore(&mcs->lock, flags);
Patrick McHardy4bd73ae2009-06-12 03:17:19 +0000879 return NETDEV_TX_OK;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700880}
881
Stephen Hemmingerddc2a922009-03-20 19:35:36 +0000882static const struct net_device_ops mcs_netdev_ops = {
883 .ndo_open = mcs_net_open,
884 .ndo_stop = mcs_net_close,
885 .ndo_start_xmit = mcs_hard_xmit,
886 .ndo_do_ioctl = mcs_net_ioctl,
887};
888
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700889/*
890 * This function is called by the USB subsystem for each new device in the
891 * system. Need to verify the device and if it is, then start handling it.
892 */
893static int mcs_probe(struct usb_interface *intf,
894 const struct usb_device_id *id)
895{
896 struct usb_device *udev = interface_to_usbdev(intf);
897 struct net_device *ndev = NULL;
898 struct mcs_cb *mcs;
899 int ret = -ENOMEM;
900
901 ndev = alloc_irdadev(sizeof(*mcs));
902 if (!ndev)
903 goto error1;
904
Joe Perches955a9d202014-11-11 14:44:57 -0800905 pr_debug("MCS7780 USB-IrDA bridge found at %d.\n", udev->devnum);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700906
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700907 SET_NETDEV_DEV(ndev, &intf->dev);
908
909 ret = usb_reset_configuration(udev);
910 if (ret != 0) {
Joe Perches6c910232014-11-11 13:37:30 -0800911 net_err_ratelimited("mcs7780: usb reset configuration failed\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700912 goto error2;
913 }
914
915 mcs = netdev_priv(ndev);
916 mcs->usbdev = udev;
917 mcs->netdev = ndev;
918 spin_lock_init(&mcs->lock);
919
920 /* Initialize QoS for this device */
921 irda_init_max_qos_capabilies(&mcs->qos);
922
923 /* That's the Rx capability. */
924 mcs->qos.baud_rate.bits &=
925 IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200
926 | IR_576000 | IR_1152000 | (IR_4000000 << 8);
927
928
929 mcs->qos.min_turn_time.bits &= qos_mtt_bits;
930 irda_qos_bits_to_value(&mcs->qos);
931
932 /* Speed change work initialisation*/
David Howellsc4028952006-11-22 14:57:56 +0000933 INIT_WORK(&mcs->work, mcs_speed_work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700934
Stephen Hemmingerddc2a922009-03-20 19:35:36 +0000935 ndev->netdev_ops = &mcs_netdev_ops;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700936
Peter Senna Tschudin67340112012-10-05 11:33:04 +0000937 if (!intf->cur_altsetting) {
938 ret = -ENOMEM;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700939 goto error2;
Peter Senna Tschudin67340112012-10-05 11:33:04 +0000940 }
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700941
942 ret = mcs_find_endpoints(mcs, intf->cur_altsetting->endpoint,
943 intf->cur_altsetting->desc.bNumEndpoints);
944 if (!ret) {
945 ret = -ENODEV;
946 goto error2;
947 }
948
949 ret = register_netdev(ndev);
950 if (ret != 0)
951 goto error2;
952
Joe Perches955a9d202014-11-11 14:44:57 -0800953 pr_debug("IrDA: Registered MosChip MCS7780 device as %s\n",
954 ndev->name);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700955
956 mcs->transceiver_type = transceiver_type;
957 mcs->sir_tweak = sir_tweak;
958 mcs->receive_mode = receive_mode;
959
960 usb_set_intfdata(intf, mcs);
961 return 0;
962
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400963error2:
964 free_netdev(ndev);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700965
Alexey Khoroshilov89473122013-09-13 00:51:20 +0400966error1:
967 return ret;
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700968}
969
970/* The current device is removed, the USB layer tells us to shut down. */
971static void mcs_disconnect(struct usb_interface *intf)
972{
973 struct mcs_cb *mcs = usb_get_intfdata(intf);
974
975 if (!mcs)
976 return;
977
Tejun Heo23f333a2010-12-12 16:45:14 +0100978 cancel_work_sync(&mcs->work);
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700979
980 unregister_netdev(mcs->netdev);
981 free_netdev(mcs->netdev);
982
983 usb_set_intfdata(intf, NULL);
Joe Perches955a9d202014-11-11 14:44:57 -0800984 pr_debug("MCS7780 now disconnected.\n");
Samuel Ortizc6ae5222006-05-25 16:19:22 -0700985}
986
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800987module_usb_driver(mcs_driver);