irport.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*********************************************************************
  2. *
  3. * Filename: irport.c
  4. * Version: 1.0
  5. * Description: Half duplex serial port SIR driver for IrDA.
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 3 13:49:59 1997
  9. * Modified at: Fri Jan 28 20:22:38 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: serial.c by Linus Torvalds
  12. *
  13. * Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes, All Rights Reserved.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (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., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. *
  31. * This driver is ment to be a small half duplex serial driver to be
  32. * used for IR-chipsets that has a UART (16550) compatibility mode.
  33. * Eventually it will replace irtty, because of irtty has some
  34. * problems that is hard to get around when we don't have control
  35. * over the serial driver. This driver may also be used by FIR
  36. * drivers to handle SIR mode for them.
  37. *
  38. ********************************************************************/
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/types.h>
  42. #include <linux/ioport.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/serial_reg.h>
  47. #include <linux/errno.h>
  48. #include <linux/init.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/delay.h>
  51. #include <linux/rtnetlink.h>
  52. #include <linux/bitops.h>
  53. #include <asm/system.h>
  54. #include <asm/io.h>
  55. #include <net/irda/irda.h>
  56. #include <net/irda/wrapper.h>
  57. #include "irport.h"
  58. #define IO_EXTENT 8
  59. /*
  60. * Currently you'll need to set these values using insmod like this:
  61. * insmod irport io=0x3e8 irq=11
  62. */
  63. static unsigned int io[] = { ~0, ~0, ~0, ~0 };
  64. static unsigned int irq[] = { 0, 0, 0, 0 };
  65. static unsigned int qos_mtt_bits = 0x03;
  66. static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL};
  67. static char *driver_name = "irport";
  68. static inline void irport_write_wakeup(struct irport_cb *self);
  69. static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len);
  70. static inline void irport_receive(struct irport_cb *self);
  71. static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq,
  72. int cmd);
  73. static inline int irport_is_receiving(struct irport_cb *self);
  74. static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts);
  75. static int irport_raw_write(struct net_device *dev, __u8 *buf, int len);
  76. static struct net_device_stats *irport_net_get_stats(struct net_device *dev);
  77. static int irport_change_speed_complete(struct irda_task *task);
  78. static void irport_timeout(struct net_device *dev);
  79. static irqreturn_t irport_interrupt(int irq, void *dev_id);
  80. static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev);
  81. static void irport_change_speed(void *priv, __u32 speed);
  82. static int irport_net_open(struct net_device *dev);
  83. static int irport_net_close(struct net_device *dev);
  84. static struct irport_cb *
  85. irport_open(int i, unsigned int iobase, unsigned int irq)
  86. {
  87. struct net_device *dev;
  88. struct irport_cb *self;
  89. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  90. /* Lock the port that we need */
  91. if (!request_region(iobase, IO_EXTENT, driver_name)) {
  92. IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
  93. __FUNCTION__, iobase);
  94. goto err_out1;
  95. }
  96. /*
  97. * Allocate new instance of the driver
  98. */
  99. dev = alloc_irdadev(sizeof(struct irport_cb));
  100. if (!dev) {
  101. IRDA_ERROR("%s(), can't allocate memory for "
  102. "irda device!\n", __FUNCTION__);
  103. goto err_out2;
  104. }
  105. self = dev->priv;
  106. spin_lock_init(&self->lock);
  107. /* Need to store self somewhere */
  108. dev_self[i] = self;
  109. self->priv = self;
  110. self->index = i;
  111. /* Initialize IO */
  112. self->io.sir_base = iobase;
  113. self->io.sir_ext = IO_EXTENT;
  114. self->io.irq = irq;
  115. self->io.fifo_size = 16; /* 16550A and compatible */
  116. /* Initialize QoS for this device */
  117. irda_init_max_qos_capabilies(&self->qos);
  118. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  119. IR_115200;
  120. self->qos.min_turn_time.bits = qos_mtt_bits;
  121. irda_qos_bits_to_value(&self->qos);
  122. /* Bootstrap ZeroCopy Rx */
  123. self->rx_buff.truesize = IRDA_SKB_MAX_MTU;
  124. self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize,
  125. GFP_KERNEL);
  126. if (self->rx_buff.skb == NULL) {
  127. IRDA_ERROR("%s(), can't allocate memory for "
  128. "receive buffer!\n", __FUNCTION__);
  129. goto err_out3;
  130. }
  131. skb_reserve(self->rx_buff.skb, 1);
  132. self->rx_buff.head = self->rx_buff.skb->data;
  133. /* No need to memset the buffer, unless you are really pedantic */
  134. /* Finish setup the Rx buffer descriptor */
  135. self->rx_buff.in_frame = FALSE;
  136. self->rx_buff.state = OUTSIDE_FRAME;
  137. self->rx_buff.data = self->rx_buff.head;
  138. /* Specify how much memory we want */
  139. self->tx_buff.truesize = 4000;
  140. /* Allocate memory if needed */
  141. if (self->tx_buff.truesize > 0) {
  142. self->tx_buff.head = kzalloc(self->tx_buff.truesize,
  143. GFP_KERNEL);
  144. if (self->tx_buff.head == NULL) {
  145. IRDA_ERROR("%s(), can't allocate memory for "
  146. "transmit buffer!\n", __FUNCTION__);
  147. goto err_out4;
  148. }
  149. }
  150. self->tx_buff.data = self->tx_buff.head;
  151. self->netdev = dev;
  152. /* May be overridden by piggyback drivers */
  153. self->interrupt = irport_interrupt;
  154. self->change_speed = irport_change_speed;
  155. /* Override the network functions we need to use */
  156. dev->hard_start_xmit = irport_hard_xmit;
  157. dev->tx_timeout = irport_timeout;
  158. dev->watchdog_timeo = HZ; /* Allow time enough for speed change */
  159. dev->open = irport_net_open;
  160. dev->stop = irport_net_close;
  161. dev->get_stats = irport_net_get_stats;
  162. dev->do_ioctl = irport_net_ioctl;
  163. /* Make ifconfig display some details */
  164. dev->base_addr = iobase;
  165. dev->irq = irq;
  166. if (register_netdev(dev)) {
  167. IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
  168. goto err_out5;
  169. }
  170. IRDA_MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
  171. dev->name, iobase, irq);
  172. return self;
  173. err_out5:
  174. kfree(self->tx_buff.head);
  175. err_out4:
  176. kfree_skb(self->rx_buff.skb);
  177. err_out3:
  178. free_netdev(dev);
  179. dev_self[i] = NULL;
  180. err_out2:
  181. release_region(iobase, IO_EXTENT);
  182. err_out1:
  183. return NULL;
  184. }
  185. static int irport_close(struct irport_cb *self)
  186. {
  187. IRDA_ASSERT(self != NULL, return -1;);
  188. /* We are not using any dongle anymore! */
  189. if (self->dongle)
  190. irda_device_dongle_cleanup(self->dongle);
  191. self->dongle = NULL;
  192. /* Remove netdevice */
  193. unregister_netdev(self->netdev);
  194. /* Release the IO-port that this driver is using */
  195. IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
  196. __FUNCTION__, self->io.sir_base);
  197. release_region(self->io.sir_base, self->io.sir_ext);
  198. kfree(self->tx_buff.head);
  199. if (self->rx_buff.skb)
  200. kfree_skb(self->rx_buff.skb);
  201. self->rx_buff.skb = NULL;
  202. /* Remove ourselves */
  203. dev_self[self->index] = NULL;
  204. free_netdev(self->netdev);
  205. return 0;
  206. }
  207. static void irport_stop(struct irport_cb *self)
  208. {
  209. int iobase;
  210. iobase = self->io.sir_base;
  211. /* We can't lock, we may be called from a FIR driver - Jean II */
  212. /* We are not transmitting any more */
  213. self->transmitting = 0;
  214. /* Reset UART */
  215. outb(0, iobase+UART_MCR);
  216. /* Turn off interrupts */
  217. outb(0, iobase+UART_IER);
  218. }
  219. static void irport_start(struct irport_cb *self)
  220. {
  221. int iobase;
  222. iobase = self->io.sir_base;
  223. irport_stop(self);
  224. /* We can't lock, we may be called from a FIR driver - Jean II */
  225. /* Initialize UART */
  226. outb(UART_LCR_WLEN8, iobase+UART_LCR); /* Reset DLAB */
  227. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
  228. /* Turn on interrups */
  229. outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
  230. }
  231. /*
  232. * Function irport_get_fcr (speed)
  233. *
  234. * Compute value of fcr
  235. *
  236. */
  237. static inline unsigned int irport_get_fcr(__u32 speed)
  238. {
  239. unsigned int fcr; /* FIFO control reg */
  240. /* Enable fifos */
  241. fcr = UART_FCR_ENABLE_FIFO;
  242. /*
  243. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  244. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  245. * about this timeout since it will always be fast enough.
  246. */
  247. if (speed < 38400)
  248. fcr |= UART_FCR_TRIGGER_1;
  249. else
  250. //fcr |= UART_FCR_TRIGGER_14;
  251. fcr |= UART_FCR_TRIGGER_8;
  252. return(fcr);
  253. }
  254. /*
  255. * Function irport_change_speed (self, speed)
  256. *
  257. * Set speed of IrDA port to specified baudrate
  258. *
  259. * This function should be called with irq off and spin-lock.
  260. */
  261. static void irport_change_speed(void *priv, __u32 speed)
  262. {
  263. struct irport_cb *self = (struct irport_cb *) priv;
  264. int iobase;
  265. unsigned int fcr; /* FIFO control reg */
  266. unsigned int lcr; /* Line control reg */
  267. int divisor;
  268. IRDA_ASSERT(self != NULL, return;);
  269. IRDA_ASSERT(speed != 0, return;);
  270. IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
  271. __FUNCTION__, speed, self->io.sir_base);
  272. /* We can't lock, we may be called from a FIR driver - Jean II */
  273. iobase = self->io.sir_base;
  274. /* Update accounting for new speed */
  275. self->io.speed = speed;
  276. /* Turn off interrupts */
  277. outb(0, iobase+UART_IER);
  278. divisor = SPEED_MAX/speed;
  279. /* Get proper fifo configuration */
  280. fcr = irport_get_fcr(speed);
  281. /* IrDA ports use 8N1 */
  282. lcr = UART_LCR_WLEN8;
  283. outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
  284. outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
  285. outb(divisor >> 8, iobase+UART_DLM);
  286. outb(lcr, iobase+UART_LCR); /* Set 8N1 */
  287. outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
  288. /* Turn on interrups */
  289. /* This will generate a fatal interrupt storm.
  290. * People calling us will do that properly - Jean II */
  291. //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
  292. }
  293. /*
  294. * Function __irport_change_speed (instance, state, param)
  295. *
  296. * State machine for changing speed of the device. We do it this way since
  297. * we cannot use schedule_timeout() when we are in interrupt context
  298. *
  299. */
  300. static int __irport_change_speed(struct irda_task *task)
  301. {
  302. struct irport_cb *self;
  303. __u32 speed = (__u32) task->param;
  304. unsigned long flags = 0;
  305. int wasunlocked = 0;
  306. int ret = 0;
  307. IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
  308. self = (struct irport_cb *) task->instance;
  309. IRDA_ASSERT(self != NULL, return -1;);
  310. /* Locking notes : this function may be called from irq context with
  311. * spinlock, via irport_write_wakeup(), or from non-interrupt without
  312. * spinlock (from the task timer). Yuck !
  313. * This is ugly, and unsafe is the spinlock is not already acquired.
  314. * This will be fixed when irda-task get rewritten.
  315. * Jean II */
  316. if (!spin_is_locked(&self->lock)) {
  317. spin_lock_irqsave(&self->lock, flags);
  318. wasunlocked = 1;
  319. }
  320. switch (task->state) {
  321. case IRDA_TASK_INIT:
  322. case IRDA_TASK_WAIT:
  323. /* Are we ready to change speed yet? */
  324. if (self->tx_buff.len > 0) {
  325. task->state = IRDA_TASK_WAIT;
  326. /* Try again later */
  327. ret = msecs_to_jiffies(20);
  328. break;
  329. }
  330. if (self->dongle)
  331. irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
  332. else
  333. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  334. break;
  335. case IRDA_TASK_CHILD_INIT:
  336. /* Go to default speed */
  337. self->change_speed(self->priv, 9600);
  338. /* Change speed of dongle */
  339. if (irda_task_execute(self->dongle,
  340. self->dongle->issue->change_speed,
  341. NULL, task, (void *) speed))
  342. {
  343. /* Dongle need more time to change its speed */
  344. irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
  345. /* Give dongle 1 sec to finish */
  346. ret = msecs_to_jiffies(1000);
  347. } else
  348. /* Child finished immediately */
  349. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  350. break;
  351. case IRDA_TASK_CHILD_WAIT:
  352. IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
  353. ret = -1;
  354. break;
  355. case IRDA_TASK_CHILD_DONE:
  356. /* Finally we are ready to change the speed */
  357. self->change_speed(self->priv, speed);
  358. irda_task_next_state(task, IRDA_TASK_DONE);
  359. break;
  360. default:
  361. IRDA_ERROR("%s(), unknown state %d\n",
  362. __FUNCTION__, task->state);
  363. irda_task_next_state(task, IRDA_TASK_DONE);
  364. ret = -1;
  365. break;
  366. }
  367. /* Put stuff in the state we found them - Jean II */
  368. if(wasunlocked) {
  369. spin_unlock_irqrestore(&self->lock, flags);
  370. }
  371. return ret;
  372. }
  373. /*
  374. * Function irport_change_speed_complete (task)
  375. *
  376. * Called when the change speed operation completes
  377. *
  378. */
  379. static int irport_change_speed_complete(struct irda_task *task)
  380. {
  381. struct irport_cb *self;
  382. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  383. self = (struct irport_cb *) task->instance;
  384. IRDA_ASSERT(self != NULL, return -1;);
  385. IRDA_ASSERT(self->netdev != NULL, return -1;);
  386. /* Finished changing speed, so we are not busy any longer */
  387. /* Signal network layer so it can try to send the frame */
  388. netif_wake_queue(self->netdev);
  389. return 0;
  390. }
  391. /*
  392. * Function irport_timeout (struct net_device *dev)
  393. *
  394. * The networking layer thinks we timed out.
  395. *
  396. */
  397. static void irport_timeout(struct net_device *dev)
  398. {
  399. struct irport_cb *self;
  400. int iobase;
  401. int iir, lsr;
  402. unsigned long flags;
  403. self = (struct irport_cb *) dev->priv;
  404. IRDA_ASSERT(self != NULL, return;);
  405. iobase = self->io.sir_base;
  406. IRDA_WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
  407. dev->name, jiffies, dev->trans_start);
  408. spin_lock_irqsave(&self->lock, flags);
  409. /* Debug what's happening... */
  410. /* Get interrupt status */
  411. lsr = inb(iobase+UART_LSR);
  412. /* Read interrupt register */
  413. iir = inb(iobase+UART_IIR);
  414. IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  415. __FUNCTION__, iir, lsr, iobase);
  416. IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%td\n",
  417. __FUNCTION__, self->transmitting, self->tx_buff.len,
  418. self->tx_buff.data - self->tx_buff.head);
  419. /* Now, restart the port */
  420. irport_start(self);
  421. self->change_speed(self->priv, self->io.speed);
  422. /* This will re-enable irqs */
  423. outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
  424. dev->trans_start = jiffies;
  425. spin_unlock_irqrestore(&self->lock, flags);
  426. netif_wake_queue(dev);
  427. }
  428. /*
  429. * Function irport_wait_hw_transmitter_finish ()
  430. *
  431. * Wait for the real end of HW transmission
  432. *
  433. * The UART is a strict FIFO, and we get called only when we have finished
  434. * pushing data to the FIFO, so the maximum amount of time we must wait
  435. * is only for the FIFO to drain out.
  436. *
  437. * We use a simple calibrated loop. We may need to adjust the loop
  438. * delay (udelay) to balance I/O traffic and latency. And we also need to
  439. * adjust the maximum timeout.
  440. * It would probably be better to wait for the proper interrupt,
  441. * but it doesn't seem to be available.
  442. *
  443. * We can't use jiffies or kernel timers because :
  444. * 1) We are called from the interrupt handler, which disable softirqs,
  445. * so jiffies won't be increased
  446. * 2) Jiffies granularity is usually very coarse (10ms), and we don't
  447. * want to wait that long to detect stuck hardware.
  448. * Jean II
  449. */
  450. static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
  451. {
  452. int iobase;
  453. int count = 1000; /* 1 ms */
  454. iobase = self->io.sir_base;
  455. /* Calibrated busy loop */
  456. while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
  457. udelay(1);
  458. if(count == 0)
  459. IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
  460. }
  461. /*
  462. * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
  463. *
  464. * Transmits the current frame until FIFO is full, then
  465. * waits until the next transmitt interrupt, and continues until the
  466. * frame is transmitted.
  467. */
  468. static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  469. {
  470. struct irport_cb *self;
  471. unsigned long flags;
  472. int iobase;
  473. s32 speed;
  474. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  475. IRDA_ASSERT(dev != NULL, return 0;);
  476. self = (struct irport_cb *) dev->priv;
  477. IRDA_ASSERT(self != NULL, return 0;);
  478. iobase = self->io.sir_base;
  479. netif_stop_queue(dev);
  480. /* Make sure tests & speed change are atomic */
  481. spin_lock_irqsave(&self->lock, flags);
  482. /* Check if we need to change the speed */
  483. speed = irda_get_next_speed(skb);
  484. if ((speed != self->io.speed) && (speed != -1)) {
  485. /* Check for empty frame */
  486. if (!skb->len) {
  487. /*
  488. * We send frames one by one in SIR mode (no
  489. * pipelining), so at this point, if we were sending
  490. * a previous frame, we just received the interrupt
  491. * telling us it is finished (UART_IIR_THRI).
  492. * Therefore, waiting for the transmitter to really
  493. * finish draining the fifo won't take too long.
  494. * And the interrupt handler is not expected to run.
  495. * - Jean II */
  496. irport_wait_hw_transmitter_finish(self);
  497. /* Better go there already locked - Jean II */
  498. irda_task_execute(self, __irport_change_speed,
  499. irport_change_speed_complete,
  500. NULL, (void *) speed);
  501. dev->trans_start = jiffies;
  502. spin_unlock_irqrestore(&self->lock, flags);
  503. dev_kfree_skb(skb);
  504. return 0;
  505. } else
  506. self->new_speed = speed;
  507. }
  508. /* Init tx buffer */
  509. self->tx_buff.data = self->tx_buff.head;
  510. /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  511. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  512. self->tx_buff.truesize);
  513. self->stats.tx_bytes += self->tx_buff.len;
  514. /* We are transmitting */
  515. self->transmitting = 1;
  516. /* Turn on transmit finished interrupt. Will fire immediately! */
  517. outb(UART_IER_THRI, iobase+UART_IER);
  518. dev->trans_start = jiffies;
  519. spin_unlock_irqrestore(&self->lock, flags);
  520. dev_kfree_skb(skb);
  521. return 0;
  522. }
  523. /*
  524. * Function irport_write (driver)
  525. *
  526. * Fill Tx FIFO with transmit data
  527. *
  528. * Called only from irport_write_wakeup()
  529. */
  530. static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
  531. {
  532. int actual = 0;
  533. /* Fill FIFO with current frame */
  534. while ((actual < fifo_size) && (actual < len)) {
  535. /* Transmit next byte */
  536. outb(buf[actual], iobase+UART_TX);
  537. actual++;
  538. }
  539. return actual;
  540. }
  541. /*
  542. * Function irport_write_wakeup (tty)
  543. *
  544. * Called by the driver when there's room for more data. If we have
  545. * more packets to send, we send them here.
  546. *
  547. * Called only from irport_interrupt()
  548. * Make sure this function is *not* called while we are receiving,
  549. * otherwise we will reset fifo and loose data :-(
  550. */
  551. static inline void irport_write_wakeup(struct irport_cb *self)
  552. {
  553. int actual = 0;
  554. int iobase;
  555. unsigned int fcr;
  556. IRDA_ASSERT(self != NULL, return;);
  557. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  558. iobase = self->io.sir_base;
  559. /* Finished with frame? */
  560. if (self->tx_buff.len > 0) {
  561. /* Write data left in transmit buffer */
  562. actual = irport_write(iobase, self->io.fifo_size,
  563. self->tx_buff.data, self->tx_buff.len);
  564. self->tx_buff.data += actual;
  565. self->tx_buff.len -= actual;
  566. } else {
  567. /*
  568. * Now serial buffer is almost free & we can start
  569. * transmission of another packet. But first we must check
  570. * if we need to change the speed of the hardware
  571. */
  572. if (self->new_speed) {
  573. irport_wait_hw_transmitter_finish(self);
  574. irda_task_execute(self, __irport_change_speed,
  575. irport_change_speed_complete,
  576. NULL, (void *) self->new_speed);
  577. self->new_speed = 0;
  578. } else {
  579. /* Tell network layer that we want more frames */
  580. netif_wake_queue(self->netdev);
  581. }
  582. self->stats.tx_packets++;
  583. /*
  584. * Reset Rx FIFO to make sure that all reflected transmit data
  585. * is discarded. This is needed for half duplex operation
  586. */
  587. fcr = irport_get_fcr(self->io.speed);
  588. fcr |= UART_FCR_CLEAR_RCVR;
  589. outb(fcr, iobase+UART_FCR);
  590. /* Finished transmitting */
  591. self->transmitting = 0;
  592. /* Turn on receive interrupts */
  593. outb(UART_IER_RDI, iobase+UART_IER);
  594. IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
  595. }
  596. }
  597. /*
  598. * Function irport_receive (self)
  599. *
  600. * Receive one frame from the infrared port
  601. *
  602. * Called only from irport_interrupt()
  603. */
  604. static inline void irport_receive(struct irport_cb *self)
  605. {
  606. int boguscount = 0;
  607. int iobase;
  608. IRDA_ASSERT(self != NULL, return;);
  609. iobase = self->io.sir_base;
  610. /*
  611. * Receive all characters in Rx FIFO, unwrap and unstuff them.
  612. * async_unwrap_char will deliver all found frames
  613. */
  614. do {
  615. async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
  616. inb(iobase+UART_RX));
  617. /* Make sure we don't stay here too long */
  618. if (boguscount++ > 32) {
  619. IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
  620. break;
  621. }
  622. } while (inb(iobase+UART_LSR) & UART_LSR_DR);
  623. }
  624. /*
  625. * Function irport_interrupt (irq, dev_id)
  626. *
  627. * Interrupt handler
  628. */
  629. static irqreturn_t irport_interrupt(int irq, void *dev_id)
  630. {
  631. struct net_device *dev = dev_id;
  632. struct irport_cb *self;
  633. int boguscount = 0;
  634. int iobase;
  635. int iir, lsr;
  636. int handled = 0;
  637. self = dev->priv;
  638. spin_lock(&self->lock);
  639. iobase = self->io.sir_base;
  640. /* Cut'n'paste interrupt routine from serial.c
  641. * This version try to minimise latency and I/O operations.
  642. * Simplified and modified to enforce half duplex operation.
  643. * - Jean II */
  644. /* Check status even is iir reg is cleared, more robust and
  645. * eliminate a read on the I/O bus - Jean II */
  646. do {
  647. /* Get interrupt status ; Clear interrupt */
  648. lsr = inb(iobase+UART_LSR);
  649. /* Are we receiving or transmitting ? */
  650. if(!self->transmitting) {
  651. /* Received something ? */
  652. if (lsr & UART_LSR_DR)
  653. irport_receive(self);
  654. } else {
  655. /* Room in Tx fifo ? */
  656. if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
  657. irport_write_wakeup(self);
  658. }
  659. /* A bit hackish, but working as expected... Jean II */
  660. if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
  661. handled = 1;
  662. /* Make sure we don't stay here to long */
  663. if (boguscount++ > 10) {
  664. IRDA_WARNING("%s() irq handler looping : lsr=%02x\n",
  665. __FUNCTION__, lsr);
  666. break;
  667. }
  668. /* Read interrupt register */
  669. iir = inb(iobase+UART_IIR);
  670. /* Enable this debug only when no other options and at low
  671. * bit rates, otherwise it may cause Rx overruns (lsr=63).
  672. * - Jean II */
  673. IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  674. __FUNCTION__, iir, lsr, iobase);
  675. /* As long as interrupt pending... */
  676. } while ((iir & UART_IIR_NO_INT) == 0);
  677. spin_unlock(&self->lock);
  678. return IRQ_RETVAL(handled);
  679. }
  680. /*
  681. * Function irport_net_open (dev)
  682. *
  683. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  684. *
  685. */
  686. static int irport_net_open(struct net_device *dev)
  687. {
  688. struct irport_cb *self;
  689. int iobase;
  690. char hwname[16];
  691. unsigned long flags;
  692. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  693. IRDA_ASSERT(dev != NULL, return -1;);
  694. self = (struct irport_cb *) dev->priv;
  695. iobase = self->io.sir_base;
  696. if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
  697. (void *) dev)) {
  698. IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
  699. __FUNCTION__, self->io.irq);
  700. return -EAGAIN;
  701. }
  702. spin_lock_irqsave(&self->lock, flags);
  703. /* Init uart */
  704. irport_start(self);
  705. /* Set 9600 bauds per default, including at the dongle */
  706. irda_task_execute(self, __irport_change_speed,
  707. irport_change_speed_complete,
  708. NULL, (void *) 9600);
  709. spin_unlock_irqrestore(&self->lock, flags);
  710. /* Give self a hardware name */
  711. sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
  712. /*
  713. * Open new IrLAP layer instance, now that everything should be
  714. * initialized properly
  715. */
  716. self->irlap = irlap_open(dev, &self->qos, hwname);
  717. /* Ready to play! */
  718. netif_start_queue(dev);
  719. return 0;
  720. }
  721. /*
  722. * Function irport_net_close (self)
  723. *
  724. * Network device is taken down. Usually this is done by
  725. * "ifconfig irda0 down"
  726. */
  727. static int irport_net_close(struct net_device *dev)
  728. {
  729. struct irport_cb *self;
  730. int iobase;
  731. unsigned long flags;
  732. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  733. IRDA_ASSERT(dev != NULL, return -1;);
  734. self = (struct irport_cb *) dev->priv;
  735. IRDA_ASSERT(self != NULL, return -1;);
  736. iobase = self->io.sir_base;
  737. /* Stop device */
  738. netif_stop_queue(dev);
  739. /* Stop and remove instance of IrLAP */
  740. if (self->irlap)
  741. irlap_close(self->irlap);
  742. self->irlap = NULL;
  743. spin_lock_irqsave(&self->lock, flags);
  744. irport_stop(self);
  745. spin_unlock_irqrestore(&self->lock, flags);
  746. free_irq(self->io.irq, dev);
  747. return 0;
  748. }
  749. /*
  750. * Function irport_is_receiving (self)
  751. *
  752. * Returns true is we are currently receiving data
  753. *
  754. */
  755. static inline int irport_is_receiving(struct irport_cb *self)
  756. {
  757. return (self->rx_buff.state != OUTSIDE_FRAME);
  758. }
  759. /*
  760. * Function irport_set_dtr_rts (tty, dtr, rts)
  761. *
  762. * This function can be used by dongles etc. to set or reset the status
  763. * of the dtr and rts lines
  764. */
  765. static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
  766. {
  767. struct irport_cb *self = dev->priv;
  768. int iobase;
  769. IRDA_ASSERT(self != NULL, return -1;);
  770. iobase = self->io.sir_base;
  771. if (dtr)
  772. dtr = UART_MCR_DTR;
  773. if (rts)
  774. rts = UART_MCR_RTS;
  775. outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
  776. return 0;
  777. }
  778. static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
  779. {
  780. struct irport_cb *self = (struct irport_cb *) dev->priv;
  781. int actual = 0;
  782. int iobase;
  783. IRDA_ASSERT(self != NULL, return -1;);
  784. iobase = self->io.sir_base;
  785. /* Tx FIFO should be empty! */
  786. if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
  787. IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
  788. return -1;
  789. }
  790. /* Fill FIFO with current frame */
  791. while (actual < len) {
  792. /* Transmit next byte */
  793. outb(buf[actual], iobase+UART_TX);
  794. actual++;
  795. }
  796. return actual;
  797. }
  798. /*
  799. * Function irport_net_ioctl (dev, rq, cmd)
  800. *
  801. * Process IOCTL commands for this device
  802. *
  803. */
  804. static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  805. {
  806. struct if_irda_req *irq = (struct if_irda_req *) rq;
  807. struct irport_cb *self;
  808. dongle_t *dongle;
  809. unsigned long flags;
  810. int ret = 0;
  811. IRDA_ASSERT(dev != NULL, return -1;);
  812. self = dev->priv;
  813. IRDA_ASSERT(self != NULL, return -1;);
  814. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
  815. switch (cmd) {
  816. case SIOCSBANDWIDTH: /* Set bandwidth */
  817. if (!capable(CAP_NET_ADMIN))
  818. ret = -EPERM;
  819. else
  820. irda_task_execute(self, __irport_change_speed, NULL,
  821. NULL, (void *) irq->ifr_baudrate);
  822. break;
  823. case SIOCSDONGLE: /* Set dongle */
  824. if (!capable(CAP_NET_ADMIN)) {
  825. ret = -EPERM;
  826. break;
  827. }
  828. /* Locking :
  829. * irda_device_dongle_init() can't be locked.
  830. * irda_task_execute() doesn't need to be locked.
  831. * Jean II
  832. */
  833. /* Initialize dongle */
  834. dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
  835. if (!dongle)
  836. break;
  837. dongle->set_mode = NULL;
  838. dongle->read = NULL;
  839. dongle->write = irport_raw_write;
  840. dongle->set_dtr_rts = irport_set_dtr_rts;
  841. /* Now initialize the dongle! */
  842. dongle->issue->open(dongle, &self->qos);
  843. /* Reset dongle */
  844. irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
  845. NULL);
  846. /* Make dongle available to driver only now to avoid
  847. * race conditions - Jean II */
  848. self->dongle = dongle;
  849. break;
  850. case SIOCSMEDIABUSY: /* Set media busy */
  851. if (!capable(CAP_NET_ADMIN)) {
  852. ret = -EPERM;
  853. break;
  854. }
  855. irda_device_set_media_busy(self->netdev, TRUE);
  856. break;
  857. case SIOCGRECEIVING: /* Check if we are receiving right now */
  858. irq->ifr_receiving = irport_is_receiving(self);
  859. break;
  860. case SIOCSDTRRTS:
  861. if (!capable(CAP_NET_ADMIN)) {
  862. ret = -EPERM;
  863. break;
  864. }
  865. /* No real need to lock... */
  866. spin_lock_irqsave(&self->lock, flags);
  867. irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
  868. spin_unlock_irqrestore(&self->lock, flags);
  869. break;
  870. default:
  871. ret = -EOPNOTSUPP;
  872. }
  873. return ret;
  874. }
  875. static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
  876. {
  877. struct irport_cb *self = (struct irport_cb *) dev->priv;
  878. return &self->stats;
  879. }
  880. static int __init irport_init(void)
  881. {
  882. int i;
  883. for (i=0; (io[i] < 2000) && (i < ARRAY_SIZE(dev_self)); i++) {
  884. if (irport_open(i, io[i], irq[i]) != NULL)
  885. return 0;
  886. }
  887. /*
  888. * Maybe something failed, but we can still be usable for FIR drivers
  889. */
  890. return 0;
  891. }
  892. /*
  893. * Function irport_cleanup ()
  894. *
  895. * Close all configured ports
  896. *
  897. */
  898. static void __exit irport_cleanup(void)
  899. {
  900. int i;
  901. IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
  902. for (i=0; i < ARRAY_SIZE(dev_self); i++) {
  903. if (dev_self[i])
  904. irport_close(dev_self[i]);
  905. }
  906. }
  907. module_param_array(io, int, NULL, 0);
  908. MODULE_PARM_DESC(io, "Base I/O addresses");
  909. module_param_array(irq, int, NULL, 0);
  910. MODULE_PARM_DESC(irq, "IRQ lines");
  911. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  912. MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
  913. MODULE_LICENSE("GPL");
  914. module_init(irport_init);
  915. module_exit(irport_cleanup);