ipoctal.c 19 KB

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