ipoctal.c 19 KB

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