irport.c 29 KB

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