ipoctal.c 18 KB

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