mcs7780.c 24 KB

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