irport.c 29 KB

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