ipoctal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /**
  2. * ipoctal.c
  3. *
  4. * driver for the GE IP-OCTAL boards
  5. *
  6. * Copyright (C) 2009-2012 CERN (www.cern.ch)
  7. * Author: Nicolas Serafini, EIC2 SA
  8. * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; version 2 of the License.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/sched.h>
  18. #include <linux/tty.h>
  19. #include <linux/serial.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/slab.h>
  22. #include <linux/atomic.h>
  23. #include <linux/io.h>
  24. #include <linux/ipack.h>
  25. #include "ipoctal.h"
  26. #include "scc2698.h"
  27. #define IP_OCTAL_ID_SPACE_VECTOR 0x41
  28. #define IP_OCTAL_NB_BLOCKS 4
  29. static const struct tty_operations ipoctal_fops;
  30. struct ipoctal_channel {
  31. struct ipoctal_stats stats;
  32. unsigned int nb_bytes;
  33. wait_queue_head_t queue;
  34. spinlock_t lock;
  35. unsigned int pointer_read;
  36. unsigned int pointer_write;
  37. atomic_t open;
  38. struct tty_port tty_port;
  39. union scc2698_channel __iomem *regs;
  40. union scc2698_block __iomem *block_regs;
  41. unsigned int board_id;
  42. unsigned char *board_write;
  43. u8 isr_rx_rdy_mask;
  44. u8 isr_tx_rdy_mask;
  45. };
  46. struct ipoctal {
  47. struct ipack_device *dev;
  48. unsigned int board_id;
  49. struct ipoctal_channel channel[NR_CHANNELS];
  50. unsigned char write;
  51. struct tty_driver *tty_drv;
  52. u8 __iomem *mem8_space;
  53. u8 __iomem *int_space;
  54. };
  55. static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
  56. {
  57. struct ipoctal_channel *channel;
  58. channel = dev_get_drvdata(tty->dev);
  59. iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
  60. return 0;
  61. }
  62. static int ipoctal_open(struct tty_struct *tty, struct file *file)
  63. {
  64. int res;
  65. struct ipoctal_channel *channel;
  66. channel = dev_get_drvdata(tty->dev);
  67. if (atomic_read(&channel->open))
  68. return -EBUSY;
  69. tty->driver_data = channel;
  70. res = tty_port_open(&channel->tty_port, tty, file);
  71. if (res)
  72. return res;
  73. atomic_inc(&channel->open);
  74. return 0;
  75. }
  76. static void ipoctal_reset_stats(struct ipoctal_stats *stats)
  77. {
  78. stats->tx = 0;
  79. stats->rx = 0;
  80. stats->rcv_break = 0;
  81. stats->framing_err = 0;
  82. stats->overrun_err = 0;
  83. stats->parity_err = 0;
  84. }
  85. static void ipoctal_free_channel(struct ipoctal_channel *channel)
  86. {
  87. ipoctal_reset_stats(&channel->stats);
  88. channel->pointer_read = 0;
  89. channel->pointer_write = 0;
  90. channel->nb_bytes = 0;
  91. }
  92. static void ipoctal_close(struct tty_struct *tty, struct file *filp)
  93. {
  94. struct ipoctal_channel *channel = tty->driver_data;
  95. tty_port_close(&channel->tty_port, tty, filp);
  96. if (atomic_dec_and_test(&channel->open))
  97. ipoctal_free_channel(channel);
  98. }
  99. static int ipoctal_get_icount(struct tty_struct *tty,
  100. struct serial_icounter_struct *icount)
  101. {
  102. struct ipoctal_channel *channel = tty->driver_data;
  103. icount->cts = 0;
  104. icount->dsr = 0;
  105. icount->rng = 0;
  106. icount->dcd = 0;
  107. icount->rx = channel->stats.rx;
  108. icount->tx = channel->stats.tx;
  109. icount->frame = channel->stats.framing_err;
  110. icount->parity = channel->stats.parity_err;
  111. icount->brk = channel->stats.rcv_break;
  112. return 0;
  113. }
  114. static void ipoctal_irq_rx(struct ipoctal_channel *channel,
  115. struct tty_struct *tty, u8 sr)
  116. {
  117. unsigned char value;
  118. unsigned char flag = TTY_NORMAL;
  119. u8 isr;
  120. do {
  121. value = ioread8(&channel->regs->r.rhr);
  122. /* Error: count statistics */
  123. if (sr & SR_ERROR) {
  124. iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
  125. if (sr & SR_OVERRUN_ERROR) {
  126. channel->stats.overrun_err++;
  127. /* Overrun doesn't affect the current character*/
  128. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  129. }
  130. if (sr & SR_PARITY_ERROR) {
  131. channel->stats.parity_err++;
  132. flag = TTY_PARITY;
  133. }
  134. if (sr & SR_FRAMING_ERROR) {
  135. channel->stats.framing_err++;
  136. flag = TTY_FRAME;
  137. }
  138. if (sr & SR_RECEIVED_BREAK) {
  139. iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
  140. channel->stats.rcv_break++;
  141. flag = TTY_BREAK;
  142. }
  143. }
  144. tty_insert_flip_char(tty, value, flag);
  145. /* Check if there are more characters in RX FIFO
  146. * If there are more, the isr register for this channel
  147. * has enabled the RxRDY|FFULL bit.
  148. */
  149. isr = ioread8(&channel->block_regs->r.isr);
  150. sr = ioread8(&channel->regs->r.sr);
  151. } while (isr & channel->isr_rx_rdy_mask);
  152. tty_flip_buffer_push(tty);
  153. }
  154. static void ipoctal_irq_tx(struct ipoctal_channel *channel)
  155. {
  156. unsigned char value;
  157. unsigned int *pointer_write = &channel->pointer_write;
  158. if (channel->nb_bytes <= 0) {
  159. channel->nb_bytes = 0;
  160. return;
  161. }
  162. value = channel->tty_port.xmit_buf[*pointer_write];
  163. iowrite8(value, &channel->regs->w.thr);
  164. channel->stats.tx++;
  165. (*pointer_write)++;
  166. *pointer_write = *pointer_write % PAGE_SIZE;
  167. channel->nb_bytes--;
  168. if ((channel->nb_bytes == 0) &&
  169. (waitqueue_active(&channel->queue))) {
  170. if (channel->board_id != IPACK1_DEVICE_ID_SBS_OCTAL_485) {
  171. *channel->board_write = 1;
  172. wake_up_interruptible(&channel->queue);
  173. }
  174. }
  175. }
  176. static void ipoctal_irq_channel(struct ipoctal_channel *channel)
  177. {
  178. u8 isr, sr;
  179. struct tty_struct *tty;
  180. /* If there is no client, skip the check */
  181. if (!atomic_read(&channel->open))
  182. return;
  183. tty = tty_port_tty_get(&channel->tty_port);
  184. if (!tty)
  185. return;
  186. /* The HW is organized in pair of channels. See which register we need
  187. * to read from */
  188. isr = ioread8(&channel->block_regs->r.isr);
  189. sr = ioread8(&channel->regs->r.sr);
  190. /* In case of RS-485, change from TX to RX when finishing TX.
  191. * Half-duplex. */
  192. if ((channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) &&
  193. (sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
  194. iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
  195. iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
  196. iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
  197. *channel->board_write = 1;
  198. wake_up_interruptible(&channel->queue);
  199. }
  200. /* RX data */
  201. if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
  202. ipoctal_irq_rx(channel, tty, sr);
  203. /* TX of each character */
  204. if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
  205. ipoctal_irq_tx(channel);
  206. tty_flip_buffer_push(tty);
  207. tty_kref_put(tty);
  208. }
  209. static irqreturn_t ipoctal_irq_handler(void *arg)
  210. {
  211. unsigned int i;
  212. struct ipoctal *ipoctal = (struct ipoctal *) arg;
  213. /* Check all channels */
  214. for (i = 0; i < NR_CHANNELS; i++)
  215. ipoctal_irq_channel(&ipoctal->channel[i]);
  216. /* Clear the IPack device interrupt */
  217. readw(ipoctal->int_space + ACK_INT_REQ0);
  218. readw(ipoctal->int_space + ACK_INT_REQ1);
  219. return IRQ_HANDLED;
  220. }
  221. static const struct tty_port_operations ipoctal_tty_port_ops = {
  222. .dtr_rts = NULL,
  223. .activate = ipoctal_port_activate,
  224. };
  225. static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
  226. unsigned int slot)
  227. {
  228. int res;
  229. int i;
  230. struct tty_driver *tty;
  231. char name[20];
  232. struct ipoctal_channel *channel;
  233. struct ipack_region *region;
  234. void __iomem *addr;
  235. union scc2698_channel __iomem *chan_regs;
  236. union scc2698_block __iomem *block_regs;
  237. ipoctal->board_id = ipoctal->dev->id_device;
  238. region = &ipoctal->dev->region[IPACK_IO_SPACE];
  239. addr = devm_ioremap_nocache(&ipoctal->dev->dev,
  240. region->start, region->size);
  241. if (!addr) {
  242. dev_err(&ipoctal->dev->dev,
  243. "Unable to map slot [%d:%d] IO space!\n",
  244. bus_nr, slot);
  245. return -EADDRNOTAVAIL;
  246. }
  247. /* Save the virtual address to access the registers easily */
  248. chan_regs =
  249. (union scc2698_channel __iomem *) addr;
  250. block_regs =
  251. (union scc2698_block __iomem *) addr;
  252. region = &ipoctal->dev->region[IPACK_INT_SPACE];
  253. ipoctal->int_space =
  254. devm_ioremap_nocache(&ipoctal->dev->dev,
  255. region->start, region->size);
  256. if (!ipoctal->int_space) {
  257. dev_err(&ipoctal->dev->dev,
  258. "Unable to map slot [%d:%d] INT space!\n",
  259. bus_nr, slot);
  260. return -EADDRNOTAVAIL;
  261. }
  262. region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
  263. ipoctal->mem8_space =
  264. devm_ioremap_nocache(&ipoctal->dev->dev,
  265. region->start, 0x8000);
  266. if (!addr) {
  267. dev_err(&ipoctal->dev->dev,
  268. "Unable to map slot [%d:%d] MEM8 space!\n",
  269. bus_nr, slot);
  270. return -EADDRNOTAVAIL;
  271. }
  272. /* Disable RX and TX before touching anything */
  273. for (i = 0; i < NR_CHANNELS ; i++) {
  274. struct ipoctal_channel *channel = &ipoctal->channel[i];
  275. channel->regs = chan_regs + i;
  276. channel->block_regs = block_regs + (i >> 1);
  277. channel->board_write = &ipoctal->write;
  278. channel->board_id = ipoctal->board_id;
  279. if (i & 1) {
  280. channel->isr_tx_rdy_mask = ISR_TxRDY_B;
  281. channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
  282. } else {
  283. channel->isr_tx_rdy_mask = ISR_TxRDY_A;
  284. channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
  285. }
  286. iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
  287. iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
  288. iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
  289. iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
  290. &channel->regs->w.mr); /* mr1 */
  291. iowrite8(0, &channel->regs->w.mr); /* mr2 */
  292. iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
  293. }
  294. for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
  295. iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
  296. iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
  297. &block_regs[i].w.opcr);
  298. iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
  299. IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
  300. &block_regs[i].w.imr);
  301. }
  302. /*
  303. * IP-OCTAL has different addresses to copy its IRQ vector.
  304. * Depending of the carrier these addresses are accesible or not.
  305. * More info in the datasheet.
  306. */
  307. ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
  308. ipoctal_irq_handler, ipoctal);
  309. /* Dummy write */
  310. iowrite8(1, ipoctal->mem8_space + 1);
  311. /* Register the TTY device */
  312. /* Each IP-OCTAL channel is a TTY port */
  313. tty = alloc_tty_driver(NR_CHANNELS);
  314. if (!tty)
  315. return -ENOMEM;
  316. /* Fill struct tty_driver with ipoctal data */
  317. tty->owner = THIS_MODULE;
  318. tty->driver_name = KBUILD_MODNAME;
  319. sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
  320. tty->name = name;
  321. tty->major = 0;
  322. tty->minor_start = 0;
  323. tty->type = TTY_DRIVER_TYPE_SERIAL;
  324. tty->subtype = SERIAL_TYPE_NORMAL;
  325. tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  326. tty->init_termios = tty_std_termios;
  327. tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  328. tty->init_termios.c_ispeed = 9600;
  329. tty->init_termios.c_ospeed = 9600;
  330. tty_set_operations(tty, &ipoctal_fops);
  331. res = tty_register_driver(tty);
  332. if (res) {
  333. dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
  334. put_tty_driver(tty);
  335. return res;
  336. }
  337. /* Save struct tty_driver for use it when uninstalling the device */
  338. ipoctal->tty_drv = tty;
  339. for (i = 0; i < NR_CHANNELS; i++) {
  340. struct device *tty_dev;
  341. channel = &ipoctal->channel[i];
  342. tty_port_init(&channel->tty_port);
  343. tty_port_alloc_xmit_buf(&channel->tty_port);
  344. channel->tty_port.ops = &ipoctal_tty_port_ops;
  345. ipoctal_reset_stats(&channel->stats);
  346. channel->nb_bytes = 0;
  347. init_waitqueue_head(&channel->queue);
  348. spin_lock_init(&channel->lock);
  349. channel->pointer_read = 0;
  350. channel->pointer_write = 0;
  351. tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
  352. if (IS_ERR(tty_dev)) {
  353. dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
  354. tty_port_destroy(&channel->tty_port);
  355. continue;
  356. }
  357. dev_set_drvdata(tty_dev, channel);
  358. /*
  359. * Enable again the RX. TX will be enabled when
  360. * there is something to send
  361. */
  362. iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
  363. }
  364. return 0;
  365. }
  366. static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
  367. const unsigned char *buf,
  368. int count)
  369. {
  370. unsigned long flags;
  371. int i;
  372. unsigned int *pointer_read = &channel->pointer_read;
  373. /* Copy the bytes from the user buffer to the internal one */
  374. for (i = 0; i < count; i++) {
  375. if (i <= (PAGE_SIZE - channel->nb_bytes)) {
  376. spin_lock_irqsave(&channel->lock, flags);
  377. channel->tty_port.xmit_buf[*pointer_read] = buf[i];
  378. *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
  379. channel->nb_bytes++;
  380. spin_unlock_irqrestore(&channel->lock, flags);
  381. } else {
  382. break;
  383. }
  384. }
  385. return i;
  386. }
  387. static int ipoctal_write_tty(struct tty_struct *tty,
  388. const unsigned char *buf, int count)
  389. {
  390. struct ipoctal_channel *channel = tty->driver_data;
  391. unsigned int char_copied;
  392. char_copied = ipoctal_copy_write_buffer(channel, buf, count);
  393. /* As the IP-OCTAL 485 only supports half duplex, do it manually */
  394. if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
  395. iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
  396. iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
  397. }
  398. /*
  399. * Send a packet and then disable TX to avoid failure after several send
  400. * operations
  401. */
  402. iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
  403. wait_event_interruptible(channel->queue, *channel->board_write);
  404. iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
  405. *channel->board_write = 0;
  406. return char_copied;
  407. }
  408. static int ipoctal_write_room(struct tty_struct *tty)
  409. {
  410. struct ipoctal_channel *channel = tty->driver_data;
  411. return PAGE_SIZE - channel->nb_bytes;
  412. }
  413. static int ipoctal_chars_in_buffer(struct tty_struct *tty)
  414. {
  415. struct ipoctal_channel *channel = tty->driver_data;
  416. return channel->nb_bytes;
  417. }
  418. static void ipoctal_set_termios(struct tty_struct *tty,
  419. struct ktermios *old_termios)
  420. {
  421. unsigned int cflag;
  422. unsigned char mr1 = 0;
  423. unsigned char mr2 = 0;
  424. unsigned char csr = 0;
  425. struct ipoctal_channel *channel = tty->driver_data;
  426. speed_t baud;
  427. cflag = tty->termios.c_cflag;
  428. /* Disable and reset everything before change the setup */
  429. iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
  430. iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
  431. iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
  432. iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
  433. iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
  434. /* Set Bits per chars */
  435. switch (cflag & CSIZE) {
  436. case CS6:
  437. mr1 |= MR1_CHRL_6_BITS;
  438. break;
  439. case CS7:
  440. mr1 |= MR1_CHRL_7_BITS;
  441. break;
  442. case CS8:
  443. default:
  444. mr1 |= MR1_CHRL_8_BITS;
  445. /* By default, select CS8 */
  446. tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
  447. break;
  448. }
  449. /* Set Parity */
  450. if (cflag & PARENB)
  451. if (cflag & PARODD)
  452. mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
  453. else
  454. mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
  455. else
  456. mr1 |= MR1_PARITY_OFF;
  457. /* Mark or space parity is not supported */
  458. tty->termios.c_cflag &= ~CMSPAR;
  459. /* Set stop bits */
  460. if (cflag & CSTOPB)
  461. mr2 |= MR2_STOP_BITS_LENGTH_2;
  462. else
  463. mr2 |= MR2_STOP_BITS_LENGTH_1;
  464. /* Set the flow control */
  465. switch (channel->board_id) {
  466. case IPACK1_DEVICE_ID_SBS_OCTAL_232:
  467. if (cflag & CRTSCTS) {
  468. mr1 |= MR1_RxRTS_CONTROL_ON;
  469. mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
  470. } else {
  471. mr1 |= MR1_RxRTS_CONTROL_OFF;
  472. mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
  473. }
  474. break;
  475. case IPACK1_DEVICE_ID_SBS_OCTAL_422:
  476. mr1 |= MR1_RxRTS_CONTROL_OFF;
  477. mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
  478. break;
  479. case IPACK1_DEVICE_ID_SBS_OCTAL_485:
  480. mr1 |= MR1_RxRTS_CONTROL_OFF;
  481. mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
  482. break;
  483. default:
  484. return;
  485. break;
  486. }
  487. baud = tty_get_baud_rate(tty);
  488. tty_termios_encode_baud_rate(&tty->termios, baud, baud);
  489. /* Set baud rate */
  490. switch (baud) {
  491. case 75:
  492. csr |= TX_CLK_75 | RX_CLK_75;
  493. break;
  494. case 110:
  495. csr |= TX_CLK_110 | RX_CLK_110;
  496. break;
  497. case 150:
  498. csr |= TX_CLK_150 | RX_CLK_150;
  499. break;
  500. case 300:
  501. csr |= TX_CLK_300 | RX_CLK_300;
  502. break;
  503. case 600:
  504. csr |= TX_CLK_600 | RX_CLK_600;
  505. break;
  506. case 1200:
  507. csr |= TX_CLK_1200 | RX_CLK_1200;
  508. break;
  509. case 1800:
  510. csr |= TX_CLK_1800 | RX_CLK_1800;
  511. break;
  512. case 2000:
  513. csr |= TX_CLK_2000 | RX_CLK_2000;
  514. break;
  515. case 2400:
  516. csr |= TX_CLK_2400 | RX_CLK_2400;
  517. break;
  518. case 4800:
  519. csr |= TX_CLK_4800 | RX_CLK_4800;
  520. break;
  521. case 9600:
  522. csr |= TX_CLK_9600 | RX_CLK_9600;
  523. break;
  524. case 19200:
  525. csr |= TX_CLK_19200 | RX_CLK_19200;
  526. break;
  527. case 38400:
  528. default:
  529. csr |= TX_CLK_38400 | RX_CLK_38400;
  530. /* In case of default, we establish 38400 bps */
  531. tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
  532. break;
  533. }
  534. mr1 |= MR1_ERROR_CHAR;
  535. mr1 |= MR1_RxINT_RxRDY;
  536. /* Write the control registers */
  537. iowrite8(mr1, &channel->regs->w.mr);
  538. iowrite8(mr2, &channel->regs->w.mr);
  539. iowrite8(csr, &channel->regs->w.csr);
  540. /* Enable again the RX */
  541. iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
  542. }
  543. static void ipoctal_hangup(struct tty_struct *tty)
  544. {
  545. unsigned long flags;
  546. struct ipoctal_channel *channel = tty->driver_data;
  547. if (channel == NULL)
  548. return;
  549. spin_lock_irqsave(&channel->lock, flags);
  550. channel->nb_bytes = 0;
  551. channel->pointer_read = 0;
  552. channel->pointer_write = 0;
  553. spin_unlock_irqrestore(&channel->lock, flags);
  554. tty_port_hangup(&channel->tty_port);
  555. iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
  556. iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
  557. iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
  558. iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
  559. iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
  560. clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
  561. wake_up_interruptible(&channel->tty_port.open_wait);
  562. }
  563. static const struct tty_operations ipoctal_fops = {
  564. .ioctl = NULL,
  565. .open = ipoctal_open,
  566. .close = ipoctal_close,
  567. .write = ipoctal_write_tty,
  568. .set_termios = ipoctal_set_termios,
  569. .write_room = ipoctal_write_room,
  570. .chars_in_buffer = ipoctal_chars_in_buffer,
  571. .get_icount = ipoctal_get_icount,
  572. .hangup = ipoctal_hangup,
  573. };
  574. static int ipoctal_probe(struct ipack_device *dev)
  575. {
  576. int res;
  577. struct ipoctal *ipoctal;
  578. ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
  579. if (ipoctal == NULL)
  580. return -ENOMEM;
  581. ipoctal->dev = dev;
  582. res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
  583. if (res)
  584. goto out_uninst;
  585. dev_set_drvdata(&dev->dev, ipoctal);
  586. return 0;
  587. out_uninst:
  588. kfree(ipoctal);
  589. return res;
  590. }
  591. static void __ipoctal_remove(struct ipoctal *ipoctal)
  592. {
  593. int i;
  594. ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
  595. for (i = 0; i < NR_CHANNELS; i++) {
  596. struct ipoctal_channel *channel = &ipoctal->channel[i];
  597. tty_unregister_device(ipoctal->tty_drv, i);
  598. tty_port_free_xmit_buf(&channel->tty_port);
  599. tty_port_destroy(&channel->tty_port);
  600. }
  601. tty_unregister_driver(ipoctal->tty_drv);
  602. put_tty_driver(ipoctal->tty_drv);
  603. kfree(ipoctal);
  604. }
  605. static void ipoctal_remove(struct ipack_device *idev)
  606. {
  607. __ipoctal_remove(dev_get_drvdata(&idev->dev));
  608. }
  609. static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
  610. { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
  611. IPACK1_DEVICE_ID_SBS_OCTAL_232) },
  612. { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
  613. IPACK1_DEVICE_ID_SBS_OCTAL_422) },
  614. { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
  615. IPACK1_DEVICE_ID_SBS_OCTAL_485) },
  616. { 0, },
  617. };
  618. MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
  619. static const struct ipack_driver_ops ipoctal_drv_ops = {
  620. .probe = ipoctal_probe,
  621. .remove = ipoctal_remove,
  622. };
  623. static struct ipack_driver driver = {
  624. .ops = &ipoctal_drv_ops,
  625. .id_table = ipoctal_ids,
  626. };
  627. static int __init ipoctal_init(void)
  628. {
  629. return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
  630. }
  631. static void __exit ipoctal_exit(void)
  632. {
  633. ipack_driver_unregister(&driver);
  634. }
  635. MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
  636. MODULE_LICENSE("GPL");
  637. module_init(ipoctal_init);
  638. module_exit(ipoctal_exit);