stir4200.c 27 KB

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