ipoctal.c 19 KB

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