ipoctal.c 19 KB

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