stir4200.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*****************************************************************************
  2. *
  3. * Filename: stir4200.c
  4. * Version: 0.4
  5. * Description: Irda SigmaTel USB Dongle
  6. * Status: Experimental
  7. * Author: Stephen Hemminger <shemminger@osdl.org>
  8. *
  9. * Based on earlier driver by Paul Stewart <stewart@parc.com>
  10. *
  11. * Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
  12. * Copyright (C) 2001, Dag Brattli <dag@brattli.net>
  13. * Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
  14. * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. *****************************************************************************/
  30. /*
  31. * This dongle does no framing, and requires polling to receive the
  32. * data. The STIr4200 has bulk in and out endpoints just like
  33. * usr-irda devices, but the data it sends and receives is raw; like
  34. * irtty, it needs to call the wrap and unwrap functions to add and
  35. * remove SOF/BOF and escape characters to/from the frame.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/kernel.h>
  40. #include <linux/types.h>
  41. #include <linux/init.h>
  42. #include <linux/time.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/slab.h>
  46. #include <linux/delay.h>
  47. #include <linux/usb.h>
  48. #include <linux/crc32.h>
  49. #include <linux/kthread.h>
  50. #include <linux/freezer.h>
  51. #include <net/irda/irda.h>
  52. #include <net/irda/irda_device.h>
  53. #include <net/irda/wrapper.h>
  54. #include <net/irda/crc.h>
  55. #include <asm/byteorder.h>
  56. #include <asm/unaligned.h>
  57. MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
  58. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");
  59. MODULE_LICENSE("GPL");
  60. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  61. module_param(qos_mtt_bits, int, 0);
  62. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  63. static int rx_sensitivity = 1; /* FIR 0..4, SIR 0..6 */
  64. module_param(rx_sensitivity, int, 0);
  65. MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-6, 0 is most sensitive)");
  66. static int tx_power = 0; /* 0 = highest ... 3 = lowest */
  67. module_param(tx_power, int, 0);
  68. MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)");
  69. #define STIR_IRDA_HEADER 4
  70. #define CTRL_TIMEOUT 100 /* milliseconds */
  71. #define TRANSMIT_TIMEOUT 200 /* milliseconds */
  72. #define STIR_FIFO_SIZE 4096
  73. #define FIFO_REGS_SIZE 3
  74. enum FirChars {
  75. FIR_CE = 0x7d,
  76. FIR_XBOF = 0x7f,
  77. FIR_EOF = 0x7e,
  78. };
  79. enum StirRequests {
  80. REQ_WRITE_REG = 0x00,
  81. REQ_READ_REG = 0x01,
  82. REQ_READ_ROM = 0x02,
  83. REQ_WRITE_SINGLE = 0x03,
  84. };
  85. /* Register offsets */
  86. enum StirRegs {
  87. REG_RSVD=0,
  88. REG_MODE,
  89. REG_PDCLK,
  90. REG_CTRL1,
  91. REG_CTRL2,
  92. REG_FIFOCTL,
  93. REG_FIFOLSB,
  94. REG_FIFOMSB,
  95. REG_DPLL,
  96. REG_IRDIG,
  97. REG_TEST=15,
  98. };
  99. enum StirModeMask {
  100. MODE_FIR = 0x80,
  101. MODE_SIR = 0x20,
  102. MODE_ASK = 0x10,
  103. MODE_FASTRX = 0x08,
  104. MODE_FFRSTEN = 0x04,
  105. MODE_NRESET = 0x02,
  106. MODE_2400 = 0x01,
  107. };
  108. enum StirPdclkMask {
  109. PDCLK_4000000 = 0x02,
  110. PDCLK_115200 = 0x09,
  111. PDCLK_57600 = 0x13,
  112. PDCLK_38400 = 0x1D,
  113. PDCLK_19200 = 0x3B,
  114. PDCLK_9600 = 0x77,
  115. PDCLK_2400 = 0xDF,
  116. };
  117. enum StirCtrl1Mask {
  118. CTRL1_SDMODE = 0x80,
  119. CTRL1_RXSLOW = 0x40,
  120. CTRL1_TXPWD = 0x10,
  121. CTRL1_RXPWD = 0x08,
  122. CTRL1_SRESET = 0x01,
  123. };
  124. enum StirCtrl2Mask {
  125. CTRL2_SPWIDTH = 0x08,
  126. CTRL2_REVID = 0x03,
  127. };
  128. enum StirFifoCtlMask {
  129. FIFOCTL_EOF = 0x80,
  130. FIFOCTL_UNDER = 0x40,
  131. FIFOCTL_OVER = 0x20,
  132. FIFOCTL_DIR = 0x10,
  133. FIFOCTL_CLR = 0x08,
  134. FIFOCTL_EMPTY = 0x04,
  135. };
  136. enum StirDiagMask {
  137. IRDIG_RXHIGH = 0x80,
  138. IRDIG_RXLOW = 0x40,
  139. };
  140. enum StirTestMask {
  141. TEST_PLLDOWN = 0x80,
  142. TEST_LOOPIR = 0x40,
  143. TEST_LOOPUSB = 0x20,
  144. TEST_TSTENA = 0x10,
  145. TEST_TSTOSC = 0x0F,
  146. };
  147. struct stir_cb {
  148. struct usb_device *usbdev; /* init: probe_irda */
  149. struct net_device *netdev; /* network layer */
  150. struct irlap_cb *irlap; /* The link layer we are binded to */
  151. struct net_device_stats stats; /* network statistics */
  152. struct qos_info qos;
  153. unsigned speed; /* Current speed */
  154. struct task_struct *thread; /* transmit thread */
  155. struct sk_buff *tx_pending;
  156. void *io_buf; /* transmit/receive buffer */
  157. __u8 *fifo_status;
  158. iobuff_t rx_buff; /* receive unwrap state machine */
  159. struct timeval rx_time;
  160. int receiving;
  161. struct urb *rx_urb;
  162. };
  163. /* These are the currently known USB ids */
  164. static struct usb_device_id dongles[] = {
  165. /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */
  166. { USB_DEVICE(0x066f, 0x4200) },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(usb, dongles);
  170. /* Send control message to set dongle register */
  171. static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
  172. {
  173. struct usb_device *dev = stir->usbdev;
  174. pr_debug("%s: write reg %d = 0x%x\n",
  175. stir->netdev->name, reg, value);
  176. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  177. REQ_WRITE_SINGLE,
  178. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
  179. value, reg, NULL, 0,
  180. CTRL_TIMEOUT);
  181. }
  182. /* Send control message to read multiple registers */
  183. static inline int read_reg(struct stir_cb *stir, __u16 reg,
  184. __u8 *data, __u16 count)
  185. {
  186. struct usb_device *dev = stir->usbdev;
  187. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  188. REQ_READ_REG,
  189. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  190. 0, reg, data, count,
  191. CTRL_TIMEOUT);
  192. }
  193. static inline int isfir(u32 speed)
  194. {
  195. return (speed == 4000000);
  196. }
  197. /*
  198. * Prepare a FIR IrDA frame for transmission to the USB dongle. The
  199. * FIR transmit frame is documented in the datasheet. It consists of
  200. * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
  201. * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
  202. * then the data escaped as follows:
  203. *
  204. * 0x7D -> 0x7D 0x5D
  205. * 0x7E -> 0x7D 0x5E
  206. * 0x7F -> 0x7D 0x5F
  207. *
  208. * Then, 4 bytes of little endian (stuffed) FCS follow, then two
  209. * trailing EOF bytes of 0x7E.
  210. */
  211. static inline __u8 *stuff_fir(__u8 *p, __u8 c)
  212. {
  213. switch(c) {
  214. case 0x7d:
  215. case 0x7e:
  216. case 0x7f:
  217. *p++ = 0x7d;
  218. c ^= IRDA_TRANS;
  219. /* fall through */
  220. default:
  221. *p++ = c;
  222. }
  223. return p;
  224. }
  225. /* Take raw data in skb and put it wrapped into buf */
  226. static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
  227. {
  228. __u8 *ptr = buf;
  229. __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
  230. __u16 wraplen;
  231. int i;
  232. /* Header */
  233. buf[0] = 0x55;
  234. buf[1] = 0xAA;
  235. ptr = buf + STIR_IRDA_HEADER;
  236. memset(ptr, 0x7f, 16);
  237. ptr += 16;
  238. /* BOF */
  239. *ptr++ = 0x7e;
  240. *ptr++ = 0x7e;
  241. /* Address / Control / Information */
  242. for (i = 0; i < skb->len; i++)
  243. ptr = stuff_fir(ptr, skb->data[i]);
  244. /* FCS */
  245. ptr = stuff_fir(ptr, fcs & 0xff);
  246. ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
  247. ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
  248. ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
  249. /* EOFs */
  250. *ptr++ = 0x7e;
  251. *ptr++ = 0x7e;
  252. /* Total length, minus the header */
  253. wraplen = (ptr - buf) - STIR_IRDA_HEADER;
  254. buf[2] = wraplen & 0xff;
  255. buf[3] = (wraplen >> 8) & 0xff;
  256. return wraplen + STIR_IRDA_HEADER;
  257. }
  258. static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
  259. {
  260. __u16 wraplen;
  261. wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
  262. STIR_FIFO_SIZE - STIR_IRDA_HEADER);
  263. buf[0] = 0x55;
  264. buf[1] = 0xAA;
  265. buf[2] = wraplen & 0xff;
  266. buf[3] = (wraplen >> 8) & 0xff;
  267. return wraplen + STIR_IRDA_HEADER;
  268. }
  269. /*
  270. * Frame is fully formed in the rx_buff so check crc
  271. * and pass up to irlap
  272. * setup for next receive
  273. */
  274. static void fir_eof(struct stir_cb *stir)
  275. {
  276. iobuff_t *rx_buff = &stir->rx_buff;
  277. int len = rx_buff->len - 4;
  278. struct sk_buff *skb, *nskb;
  279. __u32 fcs;
  280. if (unlikely(len <= 0)) {
  281. pr_debug("%s: short frame len %d\n",
  282. stir->netdev->name, len);
  283. ++stir->stats.rx_errors;
  284. ++stir->stats.rx_length_errors;
  285. return;
  286. }
  287. fcs = ~(crc32_le(~0, rx_buff->data, len));
  288. if (fcs != le32_to_cpu(get_unaligned((u32 *)(rx_buff->data+len)))) {
  289. pr_debug("crc error calc 0x%x len %d\n", fcs, len);
  290. stir->stats.rx_errors++;
  291. stir->stats.rx_crc_errors++;
  292. return;
  293. }
  294. /* if frame is short then just copy it */
  295. if (len < IRDA_RX_COPY_THRESHOLD) {
  296. nskb = dev_alloc_skb(len + 1);
  297. if (unlikely(!nskb)) {
  298. ++stir->stats.rx_dropped;
  299. return;
  300. }
  301. skb_reserve(nskb, 1);
  302. skb = nskb;
  303. skb_copy_to_linear_data(nskb, rx_buff->data, len);
  304. } else {
  305. nskb = dev_alloc_skb(rx_buff->truesize);
  306. if (unlikely(!nskb)) {
  307. ++stir->stats.rx_dropped;
  308. return;
  309. }
  310. skb_reserve(nskb, 1);
  311. skb = rx_buff->skb;
  312. rx_buff->skb = nskb;
  313. rx_buff->head = nskb->data;
  314. }
  315. skb_put(skb, len);
  316. skb_reset_mac_header(skb);
  317. skb->protocol = htons(ETH_P_IRDA);
  318. skb->dev = stir->netdev;
  319. netif_rx(skb);
  320. stir->stats.rx_packets++;
  321. stir->stats.rx_bytes += len;
  322. rx_buff->data = rx_buff->head;
  323. rx_buff->len = 0;
  324. }
  325. /* Unwrap FIR stuffed data and bump it to IrLAP */
  326. static void stir_fir_chars(struct stir_cb *stir,
  327. const __u8 *bytes, int len)
  328. {
  329. iobuff_t *rx_buff = &stir->rx_buff;
  330. int i;
  331. for (i = 0; i < len; i++) {
  332. __u8 byte = bytes[i];
  333. switch(rx_buff->state) {
  334. case OUTSIDE_FRAME:
  335. /* ignore garbage till start of frame */
  336. if (unlikely(byte != FIR_EOF))
  337. continue;
  338. /* Now receiving frame */
  339. rx_buff->state = BEGIN_FRAME;
  340. /* Time to initialize receive buffer */
  341. rx_buff->data = rx_buff->head;
  342. rx_buff->len = 0;
  343. continue;
  344. case LINK_ESCAPE:
  345. if (byte == FIR_EOF) {
  346. pr_debug("%s: got EOF after escape\n",
  347. stir->netdev->name);
  348. goto frame_error;
  349. }
  350. rx_buff->state = INSIDE_FRAME;
  351. byte ^= IRDA_TRANS;
  352. break;
  353. case BEGIN_FRAME:
  354. /* ignore multiple BOF/EOF */
  355. if (byte == FIR_EOF)
  356. continue;
  357. rx_buff->state = INSIDE_FRAME;
  358. rx_buff->in_frame = TRUE;
  359. /* fall through */
  360. case INSIDE_FRAME:
  361. switch(byte) {
  362. case FIR_CE:
  363. rx_buff->state = LINK_ESCAPE;
  364. continue;
  365. case FIR_XBOF:
  366. /* 0x7f is not used in this framing */
  367. pr_debug("%s: got XBOF without escape\n",
  368. stir->netdev->name);
  369. goto frame_error;
  370. case FIR_EOF:
  371. rx_buff->state = OUTSIDE_FRAME;
  372. rx_buff->in_frame = FALSE;
  373. fir_eof(stir);
  374. continue;
  375. }
  376. break;
  377. }
  378. /* add byte to rx buffer */
  379. if (unlikely(rx_buff->len >= rx_buff->truesize)) {
  380. pr_debug("%s: fir frame exceeds %d\n",
  381. stir->netdev->name, rx_buff->truesize);
  382. ++stir->stats.rx_over_errors;
  383. goto error_recovery;
  384. }
  385. rx_buff->data[rx_buff->len++] = byte;
  386. continue;
  387. frame_error:
  388. ++stir->stats.rx_frame_errors;
  389. error_recovery:
  390. ++stir->stats.rx_errors;
  391. rx_buff->state = OUTSIDE_FRAME;
  392. rx_buff->in_frame = FALSE;
  393. }
  394. }
  395. /* Unwrap SIR stuffed data and bump it up to IrLAP */
  396. static void stir_sir_chars(struct stir_cb *stir,
  397. const __u8 *bytes, int len)
  398. {
  399. int i;
  400. for (i = 0; i < len; i++)
  401. async_unwrap_char(stir->netdev, &stir->stats,
  402. &stir->rx_buff, bytes[i]);
  403. }
  404. static inline void unwrap_chars(struct stir_cb *stir,
  405. const __u8 *bytes, int length)
  406. {
  407. if (isfir(stir->speed))
  408. stir_fir_chars(stir, bytes, length);
  409. else
  410. stir_sir_chars(stir, bytes, length);
  411. }
  412. /* Mode parameters for each speed */
  413. static const struct {
  414. unsigned speed;
  415. __u8 pdclk;
  416. } stir_modes[] = {
  417. { 2400, PDCLK_2400 },
  418. { 9600, PDCLK_9600 },
  419. { 19200, PDCLK_19200 },
  420. { 38400, PDCLK_38400 },
  421. { 57600, PDCLK_57600 },
  422. { 115200, PDCLK_115200 },
  423. { 4000000, PDCLK_4000000 },
  424. };
  425. /*
  426. * Setup chip for speed.
  427. * Called at startup to initialize the chip
  428. * and on speed changes.
  429. *
  430. * Note: Write multiple registers doesn't appear to work
  431. */
  432. static int change_speed(struct stir_cb *stir, unsigned speed)
  433. {
  434. int i, err;
  435. __u8 mode;
  436. for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
  437. if (speed == stir_modes[i].speed)
  438. goto found;
  439. }
  440. warn("%s: invalid speed %d", stir->netdev->name, speed);
  441. return -EINVAL;
  442. found:
  443. pr_debug("speed change from %d to %d\n", stir->speed, speed);
  444. /* Reset modulator */
  445. err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
  446. if (err)
  447. goto out;
  448. /* Undocumented magic to tweak the DPLL */
  449. err = write_reg(stir, REG_DPLL, 0x15);
  450. if (err)
  451. goto out;
  452. /* Set clock */
  453. err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
  454. if (err)
  455. goto out;
  456. mode = MODE_NRESET | MODE_FASTRX;
  457. if (isfir(speed))
  458. mode |= MODE_FIR | MODE_FFRSTEN;
  459. else
  460. mode |= MODE_SIR;
  461. if (speed == 2400)
  462. mode |= MODE_2400;
  463. err = write_reg(stir, REG_MODE, mode);
  464. if (err)
  465. goto out;
  466. /* This resets TEMIC style transceiver if any. */
  467. err = write_reg(stir, REG_CTRL1,
  468. CTRL1_SDMODE | (tx_power & 3) << 1);
  469. if (err)
  470. goto out;
  471. err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
  472. if (err)
  473. goto out;
  474. /* Reset sensitivity */
  475. err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
  476. out:
  477. stir->speed = speed;
  478. return err;
  479. }
  480. /*
  481. * Called from net/core when new frame is available.
  482. */
  483. static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  484. {
  485. struct stir_cb *stir = netdev_priv(netdev);
  486. netif_stop_queue(netdev);
  487. /* the IRDA wrapping routines don't deal with non linear skb */
  488. SKB_LINEAR_ASSERT(skb);
  489. skb = xchg(&stir->tx_pending, skb);
  490. wake_up_process(stir->thread);
  491. /* this should never happen unless stop/wakeup problem */
  492. if (unlikely(skb)) {
  493. WARN_ON(1);
  494. dev_kfree_skb(skb);
  495. }
  496. return 0;
  497. }
  498. /*
  499. * Wait for the transmit FIFO to have space for next data
  500. *
  501. * If space < 0 then wait till FIFO completely drains.
  502. * FYI: can take up to 13 seconds at 2400baud.
  503. */
  504. static int fifo_txwait(struct stir_cb *stir, int space)
  505. {
  506. int err;
  507. unsigned long count, status;
  508. /* Read FIFO status and count */
  509. for(;;) {
  510. err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
  511. FIFO_REGS_SIZE);
  512. if (unlikely(err != FIFO_REGS_SIZE)) {
  513. warn("%s: FIFO register read error: %d",
  514. stir->netdev->name, err);
  515. return err;
  516. }
  517. status = stir->fifo_status[0];
  518. count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
  519. | stir->fifo_status[1];
  520. pr_debug("fifo status 0x%lx count %lu\n", status, count);
  521. /* is fifo receiving already, or empty */
  522. if (!(status & FIFOCTL_DIR)
  523. || (status & FIFOCTL_EMPTY))
  524. return 0;
  525. if (signal_pending(current))
  526. return -EINTR;
  527. /* shutting down? */
  528. if (!netif_running(stir->netdev)
  529. || !netif_device_present(stir->netdev))
  530. return -ESHUTDOWN;
  531. /* only waiting for some space */
  532. if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
  533. return 0;
  534. /* estimate transfer time for remaining chars */
  535. msleep((count * 8000) / stir->speed);
  536. }
  537. err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
  538. if (err)
  539. return err;
  540. err = write_reg(stir, REG_FIFOCTL, 0);
  541. if (err)
  542. return err;
  543. return 0;
  544. }
  545. /* Wait for turnaround delay before starting transmit. */
  546. static void turnaround_delay(const struct stir_cb *stir, long us)
  547. {
  548. long ticks;
  549. struct timeval now;
  550. if (us <= 0)
  551. return;
  552. do_gettimeofday(&now);
  553. if (now.tv_sec - stir->rx_time.tv_sec > 0)
  554. us -= USEC_PER_SEC;
  555. us -= now.tv_usec - stir->rx_time.tv_usec;
  556. if (us < 10)
  557. return;
  558. ticks = us / (1000000 / HZ);
  559. if (ticks > 0)
  560. schedule_timeout_interruptible(1 + ticks);
  561. else
  562. udelay(us);
  563. }
  564. /*
  565. * Start receiver by submitting a request to the receive pipe.
  566. * If nothing is available it will return after rx_interval.
  567. */
  568. static int receive_start(struct stir_cb *stir)
  569. {
  570. /* reset state */
  571. stir->receiving = 1;
  572. stir->rx_buff.in_frame = FALSE;
  573. stir->rx_buff.state = OUTSIDE_FRAME;
  574. stir->rx_urb->status = 0;
  575. return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
  576. }
  577. /* Stop all pending receive Urb's */
  578. static void receive_stop(struct stir_cb *stir)
  579. {
  580. stir->receiving = 0;
  581. usb_kill_urb(stir->rx_urb);
  582. if (stir->rx_buff.in_frame)
  583. stir->stats.collisions++;
  584. }
  585. /*
  586. * Wrap data in socket buffer and send it.
  587. */
  588. static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
  589. {
  590. unsigned wraplen;
  591. int first_frame = 0;
  592. /* if receiving, need to turnaround */
  593. if (stir->receiving) {
  594. receive_stop(stir);
  595. turnaround_delay(stir, irda_get_mtt(skb));
  596. first_frame = 1;
  597. }
  598. if (isfir(stir->speed))
  599. wraplen = wrap_fir_skb(skb, stir->io_buf);
  600. else
  601. wraplen = wrap_sir_skb(skb, stir->io_buf);
  602. /* check for space available in fifo */
  603. if (!first_frame)
  604. fifo_txwait(stir, wraplen);
  605. stir->stats.tx_packets++;
  606. stir->stats.tx_bytes += skb->len;
  607. stir->netdev->trans_start = jiffies;
  608. pr_debug("send %d (%d)\n", skb->len, wraplen);
  609. if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
  610. stir->io_buf, wraplen,
  611. NULL, TRANSMIT_TIMEOUT))
  612. stir->stats.tx_errors++;
  613. }
  614. /*
  615. * Transmit state machine thread
  616. */
  617. static int stir_transmit_thread(void *arg)
  618. {
  619. struct stir_cb *stir = arg;
  620. struct net_device *dev = stir->netdev;
  621. struct sk_buff *skb;
  622. while (!kthread_should_stop()) {
  623. #ifdef CONFIG_PM
  624. /* if suspending, then power off and wait */
  625. if (unlikely(freezing(current))) {
  626. if (stir->receiving)
  627. receive_stop(stir);
  628. else
  629. fifo_txwait(stir, -1);
  630. write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);
  631. refrigerator();
  632. if (change_speed(stir, stir->speed))
  633. break;
  634. }
  635. #endif
  636. /* if something to send? */
  637. skb = xchg(&stir->tx_pending, NULL);
  638. if (skb) {
  639. unsigned new_speed = irda_get_next_speed(skb);
  640. netif_wake_queue(dev);
  641. if (skb->len > 0)
  642. stir_send(stir, skb);
  643. dev_kfree_skb(skb);
  644. if ((new_speed != -1) && (stir->speed != new_speed)) {
  645. if (fifo_txwait(stir, -1) ||
  646. change_speed(stir, new_speed))
  647. break;
  648. }
  649. continue;
  650. }
  651. /* nothing to send? start receiving */
  652. if (!stir->receiving
  653. && irda_device_txqueue_empty(dev)) {
  654. /* Wait otherwise chip gets confused. */
  655. if (fifo_txwait(stir, -1))
  656. break;
  657. if (unlikely(receive_start(stir))) {
  658. if (net_ratelimit())
  659. info("%s: receive usb submit failed",
  660. stir->netdev->name);
  661. stir->receiving = 0;
  662. msleep(10);
  663. continue;
  664. }
  665. }
  666. /* sleep if nothing to send */
  667. set_current_state(TASK_INTERRUPTIBLE);
  668. schedule();
  669. }
  670. return 0;
  671. }
  672. /*
  673. * USB bulk receive completion callback.
  674. * Wakes up every ms (usb round trip) with wrapped
  675. * data.
  676. */
  677. static void stir_rcv_irq(struct urb *urb)
  678. {
  679. struct stir_cb *stir = urb->context;
  680. int err;
  681. /* in process of stopping, just drop data */
  682. if (!netif_running(stir->netdev))
  683. return;
  684. /* unlink, shutdown, unplug, other nasties */
  685. if (urb->status != 0)
  686. return;
  687. if (urb->actual_length > 0) {
  688. pr_debug("receive %d\n", urb->actual_length);
  689. unwrap_chars(stir, urb->transfer_buffer,
  690. urb->actual_length);
  691. stir->netdev->last_rx = jiffies;
  692. do_gettimeofday(&stir->rx_time);
  693. }
  694. /* kernel thread is stopping receiver don't resubmit */
  695. if (!stir->receiving)
  696. return;
  697. /* resubmit existing urb */
  698. err = usb_submit_urb(urb, GFP_ATOMIC);
  699. /* in case of error, the kernel thread will restart us */
  700. if (err) {
  701. warn("%s: usb receive submit error: %d",
  702. stir->netdev->name, err);
  703. stir->receiving = 0;
  704. wake_up_process(stir->thread);
  705. }
  706. }
  707. /*
  708. * Function stir_net_open (dev)
  709. *
  710. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  711. */
  712. static int stir_net_open(struct net_device *netdev)
  713. {
  714. struct stir_cb *stir = netdev_priv(netdev);
  715. int err;
  716. char hwname[16];
  717. err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
  718. if (err)
  719. goto err_out1;
  720. err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
  721. if (err)
  722. goto err_out1;
  723. err = change_speed(stir, 9600);
  724. if (err)
  725. goto err_out1;
  726. err = -ENOMEM;
  727. /* Initialize for SIR/FIR to copy data directly into skb. */
  728. stir->receiving = 0;
  729. stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
  730. stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  731. if (!stir->rx_buff.skb)
  732. goto err_out1;
  733. skb_reserve(stir->rx_buff.skb, 1);
  734. stir->rx_buff.head = stir->rx_buff.skb->data;
  735. do_gettimeofday(&stir->rx_time);
  736. stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  737. if (!stir->rx_urb)
  738. goto err_out2;
  739. stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
  740. if (!stir->io_buf)
  741. goto err_out3;
  742. usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
  743. usb_rcvbulkpipe(stir->usbdev, 2),
  744. stir->io_buf, STIR_FIFO_SIZE,
  745. stir_rcv_irq, stir);
  746. stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
  747. if (!stir->fifo_status)
  748. goto err_out4;
  749. /*
  750. * Now that everything should be initialized properly,
  751. * Open new IrLAP layer instance to take care of us...
  752. * Note : will send immediately a speed change...
  753. */
  754. sprintf(hwname, "usb#%d", stir->usbdev->devnum);
  755. stir->irlap = irlap_open(netdev, &stir->qos, hwname);
  756. if (!stir->irlap) {
  757. err("stir4200: irlap_open failed");
  758. goto err_out5;
  759. }
  760. /** Start kernel thread for transmit. */
  761. stir->thread = kthread_run(stir_transmit_thread, stir,
  762. "%s", stir->netdev->name);
  763. if (IS_ERR(stir->thread)) {
  764. err = PTR_ERR(stir->thread);
  765. err("stir4200: unable to start kernel thread");
  766. goto err_out6;
  767. }
  768. netif_start_queue(netdev);
  769. return 0;
  770. err_out6:
  771. irlap_close(stir->irlap);
  772. err_out5:
  773. kfree(stir->fifo_status);
  774. err_out4:
  775. kfree(stir->io_buf);
  776. err_out3:
  777. usb_free_urb(stir->rx_urb);
  778. err_out2:
  779. kfree_skb(stir->rx_buff.skb);
  780. err_out1:
  781. return err;
  782. }
  783. /*
  784. * Function stir_net_close (stir)
  785. *
  786. * Network device is taken down. Usually this is done by
  787. * "ifconfig irda0 down"
  788. */
  789. static int stir_net_close(struct net_device *netdev)
  790. {
  791. struct stir_cb *stir = netdev_priv(netdev);
  792. /* Stop transmit processing */
  793. netif_stop_queue(netdev);
  794. /* Kill transmit thread */
  795. kthread_stop(stir->thread);
  796. kfree(stir->fifo_status);
  797. /* Mop up receive urb's */
  798. usb_kill_urb(stir->rx_urb);
  799. kfree(stir->io_buf);
  800. usb_free_urb(stir->rx_urb);
  801. kfree_skb(stir->rx_buff.skb);
  802. /* Stop and remove instance of IrLAP */
  803. if (stir->irlap)
  804. irlap_close(stir->irlap);
  805. stir->irlap = NULL;
  806. return 0;
  807. }
  808. /*
  809. * IOCTLs : Extra out-of-band network commands...
  810. */
  811. static int stir_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  812. {
  813. struct if_irda_req *irq = (struct if_irda_req *) rq;
  814. struct stir_cb *stir = netdev_priv(netdev);
  815. int ret = 0;
  816. switch (cmd) {
  817. case SIOCSBANDWIDTH: /* Set bandwidth */
  818. if (!capable(CAP_NET_ADMIN))
  819. return -EPERM;
  820. /* Check if the device is still there */
  821. if (netif_device_present(stir->netdev))
  822. ret = change_speed(stir, irq->ifr_baudrate);
  823. break;
  824. case SIOCSMEDIABUSY: /* Set media busy */
  825. if (!capable(CAP_NET_ADMIN))
  826. return -EPERM;
  827. /* Check if the IrDA stack is still there */
  828. if (netif_running(stir->netdev))
  829. irda_device_set_media_busy(stir->netdev, TRUE);
  830. break;
  831. case SIOCGRECEIVING:
  832. /* Only approximately true */
  833. irq->ifr_receiving = stir->receiving;
  834. break;
  835. default:
  836. ret = -EOPNOTSUPP;
  837. }
  838. return ret;
  839. }
  840. /*
  841. * Get device stats (for /proc/net/dev and ifconfig)
  842. */
  843. static struct net_device_stats *stir_net_get_stats(struct net_device *netdev)
  844. {
  845. struct stir_cb *stir = netdev_priv(netdev);
  846. return &stir->stats;
  847. }
  848. /*
  849. * This routine is called by the USB subsystem for each new device
  850. * in the system. We need to check if the device is ours, and in
  851. * this case start handling it.
  852. * Note : it might be worth protecting this function by a global
  853. * spinlock... Or not, because maybe USB already deal with that...
  854. */
  855. static int stir_probe(struct usb_interface *intf,
  856. const struct usb_device_id *id)
  857. {
  858. struct usb_device *dev = interface_to_usbdev(intf);
  859. struct stir_cb *stir = NULL;
  860. struct net_device *net;
  861. int ret = -ENOMEM;
  862. /* Allocate network device container. */
  863. net = alloc_irdadev(sizeof(*stir));
  864. if(!net)
  865. goto err_out1;
  866. SET_MODULE_OWNER(net);
  867. SET_NETDEV_DEV(net, &intf->dev);
  868. stir = netdev_priv(net);
  869. stir->netdev = net;
  870. stir->usbdev = dev;
  871. ret = usb_reset_configuration(dev);
  872. if (ret != 0) {
  873. err("stir4200: usb reset configuration failed");
  874. goto err_out2;
  875. }
  876. printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
  877. "Vendor: %x, Product: %x\n",
  878. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  879. le16_to_cpu(dev->descriptor.idProduct));
  880. /* Initialize QoS for this device */
  881. irda_init_max_qos_capabilies(&stir->qos);
  882. /* That's the Rx capability. */
  883. stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
  884. IR_38400 | IR_57600 | IR_115200 |
  885. (IR_4000000 << 8);
  886. stir->qos.min_turn_time.bits &= qos_mtt_bits;
  887. irda_qos_bits_to_value(&stir->qos);
  888. /* Override the network functions we need to use */
  889. net->hard_start_xmit = stir_hard_xmit;
  890. net->open = stir_net_open;
  891. net->stop = stir_net_close;
  892. net->get_stats = stir_net_get_stats;
  893. net->do_ioctl = stir_net_ioctl;
  894. ret = register_netdev(net);
  895. if (ret != 0)
  896. goto err_out2;
  897. info("IrDA: Registered SigmaTel device %s", net->name);
  898. usb_set_intfdata(intf, stir);
  899. return 0;
  900. err_out2:
  901. free_netdev(net);
  902. err_out1:
  903. return ret;
  904. }
  905. /*
  906. * The current device is removed, the USB layer tell us to shut it down...
  907. */
  908. static void stir_disconnect(struct usb_interface *intf)
  909. {
  910. struct stir_cb *stir = usb_get_intfdata(intf);
  911. if (!stir)
  912. return;
  913. unregister_netdev(stir->netdev);
  914. free_netdev(stir->netdev);
  915. usb_set_intfdata(intf, NULL);
  916. }
  917. #ifdef CONFIG_PM
  918. /* USB suspend, so power off the transmitter/receiver */
  919. static int stir_suspend(struct usb_interface *intf, pm_message_t message)
  920. {
  921. struct stir_cb *stir = usb_get_intfdata(intf);
  922. netif_device_detach(stir->netdev);
  923. return 0;
  924. }
  925. /* Coming out of suspend, so reset hardware */
  926. static int stir_resume(struct usb_interface *intf)
  927. {
  928. struct stir_cb *stir = usb_get_intfdata(intf);
  929. netif_device_attach(stir->netdev);
  930. /* receiver restarted when send thread wakes up */
  931. return 0;
  932. }
  933. #endif
  934. /*
  935. * USB device callbacks
  936. */
  937. static struct usb_driver irda_driver = {
  938. .name = "stir4200",
  939. .probe = stir_probe,
  940. .disconnect = stir_disconnect,
  941. .id_table = dongles,
  942. #ifdef CONFIG_PM
  943. .suspend = stir_suspend,
  944. .resume = stir_resume,
  945. #endif
  946. };
  947. /*
  948. * Module insertion
  949. */
  950. static int __init stir_init(void)
  951. {
  952. return usb_register(&irda_driver);
  953. }
  954. module_init(stir_init);
  955. /*
  956. * Module removal
  957. */
  958. static void __exit stir_cleanup(void)
  959. {
  960. /* Deregister the driver and remove all pending instances */
  961. usb_deregister(&irda_driver);
  962. }
  963. module_exit(stir_cleanup);