PageRenderTime 1244ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/s390/char/con3215.c

https://github.com/Mengqi/linux-2.6
C | 1171 lines | 857 code | 125 blank | 189 comment | 151 complexity | a3ea6e7ba013514128ad59f22fc1da0c MD5 | raw file
  1. /*
  2. * 3215 line mode terminal driver.
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. *
  7. * Updated:
  8. * Aug-2000: Added tab support
  9. * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
  10. */
  11. #include <linux/kernel_stat.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/vt_kern.h>
  18. #include <linux/init.h>
  19. #include <linux/console.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/err.h>
  22. #include <linux/reboot.h>
  23. #include <linux/slab.h>
  24. #include <asm/ccwdev.h>
  25. #include <asm/cio.h>
  26. #include <asm/io.h>
  27. #include <asm/ebcdic.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/delay.h>
  30. #include <asm/cpcmd.h>
  31. #include <asm/setup.h>
  32. #include "ctrlchar.h"
  33. #define NR_3215 1
  34. #define NR_3215_REQ (4*NR_3215)
  35. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  36. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  37. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  38. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  39. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  40. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  41. #define RAW3215_NR_CCWS 3
  42. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  43. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  44. #define RAW3215_ACTIVE 2 /* set if the device is in use */
  45. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  46. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  47. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  48. #define RAW3215_CLOSING 32 /* set while in close process */
  49. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  50. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  51. #define RAW3215_FROZEN 256 /* set if 3215 is frozen for suspend */
  52. #define TAB_STOP_SIZE 8 /* tab stop size */
  53. /*
  54. * Request types for a 3215 device
  55. */
  56. enum raw3215_type {
  57. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  58. };
  59. /*
  60. * Request structure for a 3215 device
  61. */
  62. struct raw3215_req {
  63. enum raw3215_type type; /* type of the request */
  64. int start, len; /* start index & len in output buffer */
  65. int delayable; /* indication to wait for more data */
  66. int residual; /* residual count for read request */
  67. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  68. struct raw3215_info *info; /* pointer to main structure */
  69. struct raw3215_req *next; /* pointer to next request */
  70. } __attribute__ ((aligned(8)));
  71. struct raw3215_info {
  72. struct ccw_device *cdev; /* device for tty driver */
  73. spinlock_t *lock; /* pointer to irq lock */
  74. int flags; /* state flags */
  75. char *buffer; /* pointer to output buffer */
  76. char *inbuf; /* pointer to input buffer */
  77. int head; /* first free byte in output buffer */
  78. int count; /* number of bytes in output buffer */
  79. int written; /* number of bytes in write requests */
  80. struct tty_struct *tty; /* pointer to tty structure if present */
  81. struct raw3215_req *queued_read; /* pointer to queued read requests */
  82. struct raw3215_req *queued_write;/* pointer to queued write requests */
  83. wait_queue_head_t empty_wait; /* wait queue for flushing */
  84. struct timer_list timer; /* timer for delayed output */
  85. int line_pos; /* position on the line (for tabs) */
  86. char ubuffer[80]; /* copy_from_user buffer */
  87. };
  88. /* array of 3215 devices structures */
  89. static struct raw3215_info *raw3215[NR_3215];
  90. /* spinlock to protect the raw3215 array */
  91. static DEFINE_SPINLOCK(raw3215_device_lock);
  92. /* list of free request structures */
  93. static struct raw3215_req *raw3215_freelist;
  94. /* spinlock to protect free list */
  95. static spinlock_t raw3215_freelist_lock;
  96. static struct tty_driver *tty3215_driver;
  97. /*
  98. * Get a request structure from the free list
  99. */
  100. static inline struct raw3215_req *raw3215_alloc_req(void)
  101. {
  102. struct raw3215_req *req;
  103. unsigned long flags;
  104. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  105. req = raw3215_freelist;
  106. raw3215_freelist = req->next;
  107. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  108. return req;
  109. }
  110. /*
  111. * Put a request structure back to the free list
  112. */
  113. static inline void raw3215_free_req(struct raw3215_req *req)
  114. {
  115. unsigned long flags;
  116. if (req->type == RAW3215_FREE)
  117. return; /* don't free a free request */
  118. req->type = RAW3215_FREE;
  119. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  120. req->next = raw3215_freelist;
  121. raw3215_freelist = req;
  122. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  123. }
  124. /*
  125. * Set up a read request that reads up to 160 byte from the 3215 device.
  126. * If there is a queued read request it is used, but that shouldn't happen
  127. * because a 3215 terminal won't accept a new read before the old one is
  128. * completed.
  129. */
  130. static void raw3215_mk_read_req(struct raw3215_info *raw)
  131. {
  132. struct raw3215_req *req;
  133. struct ccw1 *ccw;
  134. /* there can only be ONE read request at a time */
  135. req = raw->queued_read;
  136. if (req == NULL) {
  137. /* no queued read request, use new req structure */
  138. req = raw3215_alloc_req();
  139. req->type = RAW3215_READ;
  140. req->info = raw;
  141. raw->queued_read = req;
  142. }
  143. ccw = req->ccws;
  144. ccw->cmd_code = 0x0A; /* read inquiry */
  145. ccw->flags = 0x20; /* ignore incorrect length */
  146. ccw->count = 160;
  147. ccw->cda = (__u32) __pa(raw->inbuf);
  148. }
  149. /*
  150. * Set up a write request with the information from the main structure.
  151. * A ccw chain is created that writes as much as possible from the output
  152. * buffer to the 3215 device. If a queued write exists it is replaced by
  153. * the new, probably lengthened request.
  154. */
  155. static void raw3215_mk_write_req(struct raw3215_info *raw)
  156. {
  157. struct raw3215_req *req;
  158. struct ccw1 *ccw;
  159. int len, count, ix, lines;
  160. if (raw->count <= raw->written)
  161. return;
  162. /* check if there is a queued write request */
  163. req = raw->queued_write;
  164. if (req == NULL) {
  165. /* no queued write request, use new req structure */
  166. req = raw3215_alloc_req();
  167. req->type = RAW3215_WRITE;
  168. req->info = raw;
  169. raw->queued_write = req;
  170. } else {
  171. raw->written -= req->len;
  172. }
  173. ccw = req->ccws;
  174. req->start = (raw->head - raw->count + raw->written) &
  175. (RAW3215_BUFFER_SIZE - 1);
  176. /*
  177. * now we have to count newlines. We can at max accept
  178. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  179. * a restriction in VM
  180. */
  181. lines = 0;
  182. ix = req->start;
  183. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  184. if (raw->buffer[ix] == 0x15)
  185. lines++;
  186. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  187. }
  188. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  189. if (len > RAW3215_MAX_BYTES)
  190. len = RAW3215_MAX_BYTES;
  191. req->len = len;
  192. raw->written += len;
  193. /* set the indication if we should try to enlarge this request */
  194. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  195. ix = req->start;
  196. while (len > 0) {
  197. if (ccw > req->ccws)
  198. ccw[-1].flags |= 0x40; /* use command chaining */
  199. ccw->cmd_code = 0x01; /* write, auto carrier return */
  200. ccw->flags = 0x20; /* ignore incorrect length ind. */
  201. ccw->cda =
  202. (__u32) __pa(raw->buffer + ix);
  203. count = len;
  204. if (ix + count > RAW3215_BUFFER_SIZE)
  205. count = RAW3215_BUFFER_SIZE - ix;
  206. ccw->count = count;
  207. len -= count;
  208. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  209. ccw++;
  210. }
  211. /*
  212. * Add a NOP to the channel program. 3215 devices are purely
  213. * emulated and its much better to avoid the channel end
  214. * interrupt in this case.
  215. */
  216. if (ccw > req->ccws)
  217. ccw[-1].flags |= 0x40; /* use command chaining */
  218. ccw->cmd_code = 0x03; /* NOP */
  219. ccw->flags = 0;
  220. ccw->cda = 0;
  221. ccw->count = 1;
  222. }
  223. /*
  224. * Start a read or a write request
  225. */
  226. static void raw3215_start_io(struct raw3215_info *raw)
  227. {
  228. struct raw3215_req *req;
  229. int res;
  230. req = raw->queued_read;
  231. if (req != NULL &&
  232. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  233. /* dequeue request */
  234. raw->queued_read = NULL;
  235. res = ccw_device_start(raw->cdev, req->ccws,
  236. (unsigned long) req, 0, 0);
  237. if (res != 0) {
  238. /* do_IO failed, put request back to queue */
  239. raw->queued_read = req;
  240. } else {
  241. raw->flags |= RAW3215_WORKING;
  242. }
  243. }
  244. req = raw->queued_write;
  245. if (req != NULL &&
  246. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  247. /* dequeue request */
  248. raw->queued_write = NULL;
  249. res = ccw_device_start(raw->cdev, req->ccws,
  250. (unsigned long) req, 0, 0);
  251. if (res != 0) {
  252. /* do_IO failed, put request back to queue */
  253. raw->queued_write = req;
  254. } else {
  255. raw->flags |= RAW3215_WORKING;
  256. }
  257. }
  258. }
  259. /*
  260. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  261. */
  262. static void raw3215_timeout(unsigned long __data)
  263. {
  264. struct raw3215_info *raw = (struct raw3215_info *) __data;
  265. unsigned long flags;
  266. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  267. if (raw->flags & RAW3215_TIMER_RUNS) {
  268. del_timer(&raw->timer);
  269. raw->flags &= ~RAW3215_TIMER_RUNS;
  270. if (!(raw->flags & RAW3215_FROZEN)) {
  271. raw3215_mk_write_req(raw);
  272. raw3215_start_io(raw);
  273. }
  274. }
  275. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  276. }
  277. /*
  278. * Function to conditionally start an IO. A read is started immediately,
  279. * a write is only started immediately if the flush flag is on or the
  280. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  281. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  282. */
  283. static inline void raw3215_try_io(struct raw3215_info *raw)
  284. {
  285. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FROZEN))
  286. return;
  287. if (raw->queued_read != NULL)
  288. raw3215_start_io(raw);
  289. else if (raw->queued_write != NULL) {
  290. if ((raw->queued_write->delayable == 0) ||
  291. (raw->flags & RAW3215_FLUSHING)) {
  292. /* execute write requests bigger than minimum size */
  293. raw3215_start_io(raw);
  294. if (raw->flags & RAW3215_TIMER_RUNS) {
  295. del_timer(&raw->timer);
  296. raw->flags &= ~RAW3215_TIMER_RUNS;
  297. }
  298. } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
  299. /* delay small writes */
  300. init_timer(&raw->timer);
  301. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  302. raw->timer.data = (unsigned long) raw;
  303. raw->timer.function = raw3215_timeout;
  304. add_timer(&raw->timer);
  305. raw->flags |= RAW3215_TIMER_RUNS;
  306. }
  307. }
  308. }
  309. /*
  310. * Try to start the next IO and wake up processes waiting on the tty.
  311. */
  312. static void raw3215_next_io(struct raw3215_info *raw)
  313. {
  314. struct tty_struct *tty;
  315. raw3215_mk_write_req(raw);
  316. raw3215_try_io(raw);
  317. tty = raw->tty;
  318. if (tty != NULL &&
  319. RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
  320. tty_wakeup(tty);
  321. }
  322. }
  323. /*
  324. * Interrupt routine, called from common io layer
  325. */
  326. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  327. struct irb *irb)
  328. {
  329. struct raw3215_info *raw;
  330. struct raw3215_req *req;
  331. struct tty_struct *tty;
  332. int cstat, dstat;
  333. int count;
  334. kstat_cpu(smp_processor_id()).irqs[IOINT_C15]++;
  335. raw = dev_get_drvdata(&cdev->dev);
  336. req = (struct raw3215_req *) intparm;
  337. cstat = irb->scsw.cmd.cstat;
  338. dstat = irb->scsw.cmd.dstat;
  339. if (cstat != 0)
  340. raw3215_next_io(raw);
  341. if (dstat & 0x01) { /* we got a unit exception */
  342. dstat &= ~0x01; /* we can ignore it */
  343. }
  344. switch (dstat) {
  345. case 0x80:
  346. if (cstat != 0)
  347. break;
  348. /* Attention interrupt, someone hit the enter key */
  349. raw3215_mk_read_req(raw);
  350. raw3215_next_io(raw);
  351. break;
  352. case 0x08:
  353. case 0x0C:
  354. /* Channel end interrupt. */
  355. if ((raw = req->info) == NULL)
  356. return; /* That shouldn't happen ... */
  357. if (req->type == RAW3215_READ) {
  358. /* store residual count, then wait for device end */
  359. req->residual = irb->scsw.cmd.count;
  360. }
  361. if (dstat == 0x08)
  362. break;
  363. case 0x04:
  364. /* Device end interrupt. */
  365. if ((raw = req->info) == NULL)
  366. return; /* That shouldn't happen ... */
  367. if (req->type == RAW3215_READ && raw->tty != NULL) {
  368. unsigned int cchar;
  369. tty = raw->tty;
  370. count = 160 - req->residual;
  371. EBCASC(raw->inbuf, count);
  372. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  373. switch (cchar & CTRLCHAR_MASK) {
  374. case CTRLCHAR_SYSRQ:
  375. break;
  376. case CTRLCHAR_CTRL:
  377. tty_insert_flip_char(tty, cchar, TTY_NORMAL);
  378. tty_flip_buffer_push(raw->tty);
  379. break;
  380. case CTRLCHAR_NONE:
  381. if (count < 2 ||
  382. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  383. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  384. /* add the auto \n */
  385. raw->inbuf[count] = '\n';
  386. count++;
  387. } else
  388. count -= 2;
  389. tty_insert_flip_string(tty, raw->inbuf, count);
  390. tty_flip_buffer_push(raw->tty);
  391. break;
  392. }
  393. } else if (req->type == RAW3215_WRITE) {
  394. raw->count -= req->len;
  395. raw->written -= req->len;
  396. }
  397. raw->flags &= ~RAW3215_WORKING;
  398. raw3215_free_req(req);
  399. /* check for empty wait */
  400. if (waitqueue_active(&raw->empty_wait) &&
  401. raw->queued_write == NULL &&
  402. raw->queued_read == NULL) {
  403. wake_up_interruptible(&raw->empty_wait);
  404. }
  405. raw3215_next_io(raw);
  406. break;
  407. default:
  408. /* Strange interrupt, I'll do my best to clean up */
  409. if (req != NULL && req->type != RAW3215_FREE) {
  410. if (req->type == RAW3215_WRITE) {
  411. raw->count -= req->len;
  412. raw->written -= req->len;
  413. }
  414. raw->flags &= ~RAW3215_WORKING;
  415. raw3215_free_req(req);
  416. }
  417. raw3215_next_io(raw);
  418. }
  419. return;
  420. }
  421. /*
  422. * Drop the oldest line from the output buffer.
  423. */
  424. static void raw3215_drop_line(struct raw3215_info *raw)
  425. {
  426. int ix;
  427. char ch;
  428. BUG_ON(raw->written != 0);
  429. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  430. while (raw->count > 0) {
  431. ch = raw->buffer[ix];
  432. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  433. raw->count--;
  434. if (ch == 0x15)
  435. break;
  436. }
  437. raw->head = ix;
  438. }
  439. /*
  440. * Wait until length bytes are available int the output buffer.
  441. * Has to be called with the s390irq lock held. Can be called
  442. * disabled.
  443. */
  444. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  445. {
  446. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  447. /* While console is frozen for suspend we have no other
  448. * choice but to drop message from the buffer to make
  449. * room for even more messages. */
  450. if (raw->flags & RAW3215_FROZEN) {
  451. raw3215_drop_line(raw);
  452. continue;
  453. }
  454. /* there might be a request pending */
  455. raw->flags |= RAW3215_FLUSHING;
  456. raw3215_mk_write_req(raw);
  457. raw3215_try_io(raw);
  458. raw->flags &= ~RAW3215_FLUSHING;
  459. #ifdef CONFIG_TN3215_CONSOLE
  460. wait_cons_dev();
  461. #endif
  462. /* Enough room freed up ? */
  463. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  464. break;
  465. /* there might be another cpu waiting for the lock */
  466. spin_unlock(get_ccwdev_lock(raw->cdev));
  467. udelay(100);
  468. spin_lock(get_ccwdev_lock(raw->cdev));
  469. }
  470. }
  471. /*
  472. * String write routine for 3215 devices
  473. */
  474. static void raw3215_write(struct raw3215_info *raw, const char *str,
  475. unsigned int length)
  476. {
  477. unsigned long flags;
  478. int c, count;
  479. while (length > 0) {
  480. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  481. count = (length > RAW3215_BUFFER_SIZE) ?
  482. RAW3215_BUFFER_SIZE : length;
  483. length -= count;
  484. raw3215_make_room(raw, count);
  485. /* copy string to output buffer and convert it to EBCDIC */
  486. while (1) {
  487. c = min_t(int, count,
  488. min(RAW3215_BUFFER_SIZE - raw->count,
  489. RAW3215_BUFFER_SIZE - raw->head));
  490. if (c <= 0)
  491. break;
  492. memcpy(raw->buffer + raw->head, str, c);
  493. ASCEBC(raw->buffer + raw->head, c);
  494. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  495. raw->count += c;
  496. raw->line_pos += c;
  497. str += c;
  498. count -= c;
  499. }
  500. if (!(raw->flags & RAW3215_WORKING)) {
  501. raw3215_mk_write_req(raw);
  502. /* start or queue request */
  503. raw3215_try_io(raw);
  504. }
  505. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  506. }
  507. }
  508. /*
  509. * Put character routine for 3215 devices
  510. */
  511. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  512. {
  513. unsigned long flags;
  514. unsigned int length, i;
  515. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  516. if (ch == '\t') {
  517. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  518. raw->line_pos += length;
  519. ch = ' ';
  520. } else if (ch == '\n') {
  521. length = 1;
  522. raw->line_pos = 0;
  523. } else {
  524. length = 1;
  525. raw->line_pos++;
  526. }
  527. raw3215_make_room(raw, length);
  528. for (i = 0; i < length; i++) {
  529. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  530. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  531. raw->count++;
  532. }
  533. if (!(raw->flags & RAW3215_WORKING)) {
  534. raw3215_mk_write_req(raw);
  535. /* start or queue request */
  536. raw3215_try_io(raw);
  537. }
  538. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  539. }
  540. /*
  541. * Flush routine, it simply sets the flush flag and tries to start
  542. * pending IO.
  543. */
  544. static void raw3215_flush_buffer(struct raw3215_info *raw)
  545. {
  546. unsigned long flags;
  547. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  548. if (raw->count > 0) {
  549. raw->flags |= RAW3215_FLUSHING;
  550. raw3215_try_io(raw);
  551. raw->flags &= ~RAW3215_FLUSHING;
  552. }
  553. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  554. }
  555. /*
  556. * Fire up a 3215 device.
  557. */
  558. static int raw3215_startup(struct raw3215_info *raw)
  559. {
  560. unsigned long flags;
  561. if (raw->flags & RAW3215_ACTIVE)
  562. return 0;
  563. raw->line_pos = 0;
  564. raw->flags |= RAW3215_ACTIVE;
  565. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  566. raw3215_try_io(raw);
  567. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  568. return 0;
  569. }
  570. /*
  571. * Shutdown a 3215 device.
  572. */
  573. static void raw3215_shutdown(struct raw3215_info *raw)
  574. {
  575. DECLARE_WAITQUEUE(wait, current);
  576. unsigned long flags;
  577. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
  578. return;
  579. /* Wait for outstanding requests, then free irq */
  580. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  581. if ((raw->flags & RAW3215_WORKING) ||
  582. raw->queued_write != NULL ||
  583. raw->queued_read != NULL) {
  584. raw->flags |= RAW3215_CLOSING;
  585. add_wait_queue(&raw->empty_wait, &wait);
  586. set_current_state(TASK_INTERRUPTIBLE);
  587. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  588. schedule();
  589. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  590. remove_wait_queue(&raw->empty_wait, &wait);
  591. set_current_state(TASK_RUNNING);
  592. raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
  593. }
  594. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  595. }
  596. static int raw3215_probe (struct ccw_device *cdev)
  597. {
  598. struct raw3215_info *raw;
  599. int line;
  600. /* Console is special. */
  601. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  602. return 0;
  603. raw = kmalloc(sizeof(struct raw3215_info) +
  604. RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
  605. if (raw == NULL)
  606. return -ENOMEM;
  607. spin_lock(&raw3215_device_lock);
  608. for (line = 0; line < NR_3215; line++) {
  609. if (!raw3215[line]) {
  610. raw3215[line] = raw;
  611. break;
  612. }
  613. }
  614. spin_unlock(&raw3215_device_lock);
  615. if (line == NR_3215) {
  616. kfree(raw);
  617. return -ENODEV;
  618. }
  619. raw->cdev = cdev;
  620. raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
  621. memset(raw, 0, sizeof(struct raw3215_info));
  622. raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
  623. GFP_KERNEL|GFP_DMA);
  624. if (raw->buffer == NULL) {
  625. spin_lock(&raw3215_device_lock);
  626. raw3215[line] = NULL;
  627. spin_unlock(&raw3215_device_lock);
  628. kfree(raw);
  629. return -ENOMEM;
  630. }
  631. init_waitqueue_head(&raw->empty_wait);
  632. dev_set_drvdata(&cdev->dev, raw);
  633. cdev->handler = raw3215_irq;
  634. return 0;
  635. }
  636. static void raw3215_remove (struct ccw_device *cdev)
  637. {
  638. struct raw3215_info *raw;
  639. ccw_device_set_offline(cdev);
  640. raw = dev_get_drvdata(&cdev->dev);
  641. if (raw) {
  642. dev_set_drvdata(&cdev->dev, NULL);
  643. kfree(raw->buffer);
  644. kfree(raw);
  645. }
  646. }
  647. static int raw3215_set_online (struct ccw_device *cdev)
  648. {
  649. struct raw3215_info *raw;
  650. raw = dev_get_drvdata(&cdev->dev);
  651. if (!raw)
  652. return -ENODEV;
  653. return raw3215_startup(raw);
  654. }
  655. static int raw3215_set_offline (struct ccw_device *cdev)
  656. {
  657. struct raw3215_info *raw;
  658. raw = dev_get_drvdata(&cdev->dev);
  659. if (!raw)
  660. return -ENODEV;
  661. raw3215_shutdown(raw);
  662. return 0;
  663. }
  664. static int raw3215_pm_stop(struct ccw_device *cdev)
  665. {
  666. struct raw3215_info *raw;
  667. unsigned long flags;
  668. /* Empty the output buffer, then prevent new I/O. */
  669. raw = dev_get_drvdata(&cdev->dev);
  670. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  671. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  672. raw->flags |= RAW3215_FROZEN;
  673. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  674. return 0;
  675. }
  676. static int raw3215_pm_start(struct ccw_device *cdev)
  677. {
  678. struct raw3215_info *raw;
  679. unsigned long flags;
  680. /* Allow I/O again and flush output buffer. */
  681. raw = dev_get_drvdata(&cdev->dev);
  682. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  683. raw->flags &= ~RAW3215_FROZEN;
  684. raw->flags |= RAW3215_FLUSHING;
  685. raw3215_try_io(raw);
  686. raw->flags &= ~RAW3215_FLUSHING;
  687. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  688. return 0;
  689. }
  690. static struct ccw_device_id raw3215_id[] = {
  691. { CCW_DEVICE(0x3215, 0) },
  692. { /* end of list */ },
  693. };
  694. static struct ccw_driver raw3215_ccw_driver = {
  695. .driver = {
  696. .name = "3215",
  697. .owner = THIS_MODULE,
  698. },
  699. .ids = raw3215_id,
  700. .probe = &raw3215_probe,
  701. .remove = &raw3215_remove,
  702. .set_online = &raw3215_set_online,
  703. .set_offline = &raw3215_set_offline,
  704. .freeze = &raw3215_pm_stop,
  705. .thaw = &raw3215_pm_start,
  706. .restore = &raw3215_pm_start,
  707. };
  708. #ifdef CONFIG_TN3215_CONSOLE
  709. /*
  710. * Write a string to the 3215 console
  711. */
  712. static void con3215_write(struct console *co, const char *str,
  713. unsigned int count)
  714. {
  715. struct raw3215_info *raw;
  716. int i;
  717. if (count <= 0)
  718. return;
  719. raw = raw3215[0]; /* console 3215 is the first one */
  720. while (count > 0) {
  721. for (i = 0; i < count; i++)
  722. if (str[i] == '\t' || str[i] == '\n')
  723. break;
  724. raw3215_write(raw, str, i);
  725. count -= i;
  726. str += i;
  727. if (count > 0) {
  728. raw3215_putchar(raw, *str);
  729. count--;
  730. str++;
  731. }
  732. }
  733. }
  734. static struct tty_driver *con3215_device(struct console *c, int *index)
  735. {
  736. *index = c->index;
  737. return tty3215_driver;
  738. }
  739. /*
  740. * panic() calls con3215_flush through a panic_notifier
  741. * before the system enters a disabled, endless loop.
  742. */
  743. static void con3215_flush(void)
  744. {
  745. struct raw3215_info *raw;
  746. unsigned long flags;
  747. raw = raw3215[0]; /* console 3215 is the first one */
  748. if (raw->flags & RAW3215_FROZEN)
  749. /* The console is still frozen for suspend. */
  750. if (ccw_device_force_console())
  751. /* Forcing didn't work, no panic message .. */
  752. return;
  753. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  754. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  755. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  756. }
  757. static int con3215_notify(struct notifier_block *self,
  758. unsigned long event, void *data)
  759. {
  760. con3215_flush();
  761. return NOTIFY_OK;
  762. }
  763. static struct notifier_block on_panic_nb = {
  764. .notifier_call = con3215_notify,
  765. .priority = 0,
  766. };
  767. static struct notifier_block on_reboot_nb = {
  768. .notifier_call = con3215_notify,
  769. .priority = 0,
  770. };
  771. /*
  772. * The console structure for the 3215 console
  773. */
  774. static struct console con3215 = {
  775. .name = "ttyS",
  776. .write = con3215_write,
  777. .device = con3215_device,
  778. .flags = CON_PRINTBUFFER,
  779. };
  780. /*
  781. * 3215 console initialization code called from console_init().
  782. */
  783. static int __init con3215_init(void)
  784. {
  785. struct ccw_device *cdev;
  786. struct raw3215_info *raw;
  787. struct raw3215_req *req;
  788. int i;
  789. /* Check if 3215 is to be the console */
  790. if (!CONSOLE_IS_3215)
  791. return -ENODEV;
  792. /* Set the console mode for VM */
  793. if (MACHINE_IS_VM) {
  794. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  795. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  796. }
  797. /* allocate 3215 request structures */
  798. raw3215_freelist = NULL;
  799. spin_lock_init(&raw3215_freelist_lock);
  800. for (i = 0; i < NR_3215_REQ; i++) {
  801. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  802. req->next = raw3215_freelist;
  803. raw3215_freelist = req;
  804. }
  805. cdev = ccw_device_probe_console();
  806. if (IS_ERR(cdev))
  807. return -ENODEV;
  808. raw3215[0] = raw = (struct raw3215_info *)
  809. kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  810. raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  811. raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  812. raw->cdev = cdev;
  813. dev_set_drvdata(&cdev->dev, raw);
  814. cdev->handler = raw3215_irq;
  815. raw->flags |= RAW3215_FIXED;
  816. init_waitqueue_head(&raw->empty_wait);
  817. /* Request the console irq */
  818. if (raw3215_startup(raw) != 0) {
  819. kfree(raw->inbuf);
  820. kfree(raw->buffer);
  821. kfree(raw);
  822. raw3215[0] = NULL;
  823. return -ENODEV;
  824. }
  825. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  826. register_reboot_notifier(&on_reboot_nb);
  827. register_console(&con3215);
  828. return 0;
  829. }
  830. console_initcall(con3215_init);
  831. #endif
  832. /*
  833. * tty3215_open
  834. *
  835. * This routine is called whenever a 3215 tty is opened.
  836. */
  837. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  838. {
  839. struct raw3215_info *raw;
  840. int retval, line;
  841. line = tty->index;
  842. if ((line < 0) || (line >= NR_3215))
  843. return -ENODEV;
  844. raw = raw3215[line];
  845. if (raw == NULL)
  846. return -ENODEV;
  847. tty->driver_data = raw;
  848. raw->tty = tty;
  849. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  850. /*
  851. * Start up 3215 device
  852. */
  853. retval = raw3215_startup(raw);
  854. if (retval)
  855. return retval;
  856. return 0;
  857. }
  858. /*
  859. * tty3215_close()
  860. *
  861. * This routine is called when the 3215 tty is closed. We wait
  862. * for the remaining request to be completed. Then we clean up.
  863. */
  864. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  865. {
  866. struct raw3215_info *raw;
  867. raw = (struct raw3215_info *) tty->driver_data;
  868. if (raw == NULL || tty->count > 1)
  869. return;
  870. tty->closing = 1;
  871. /* Shutdown the terminal */
  872. raw3215_shutdown(raw);
  873. tty->closing = 0;
  874. raw->tty = NULL;
  875. }
  876. /*
  877. * Returns the amount of free space in the output buffer.
  878. */
  879. static int tty3215_write_room(struct tty_struct *tty)
  880. {
  881. struct raw3215_info *raw;
  882. raw = (struct raw3215_info *) tty->driver_data;
  883. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  884. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  885. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  886. else
  887. return 0;
  888. }
  889. /*
  890. * String write routine for 3215 ttys
  891. */
  892. static int tty3215_write(struct tty_struct * tty,
  893. const unsigned char *buf, int count)
  894. {
  895. struct raw3215_info *raw;
  896. if (!tty)
  897. return 0;
  898. raw = (struct raw3215_info *) tty->driver_data;
  899. raw3215_write(raw, buf, count);
  900. return count;
  901. }
  902. /*
  903. * Put character routine for 3215 ttys
  904. */
  905. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  906. {
  907. struct raw3215_info *raw;
  908. if (!tty)
  909. return 0;
  910. raw = (struct raw3215_info *) tty->driver_data;
  911. raw3215_putchar(raw, ch);
  912. return 1;
  913. }
  914. static void tty3215_flush_chars(struct tty_struct *tty)
  915. {
  916. }
  917. /*
  918. * Returns the number of characters in the output buffer
  919. */
  920. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  921. {
  922. struct raw3215_info *raw;
  923. raw = (struct raw3215_info *) tty->driver_data;
  924. return raw->count;
  925. }
  926. static void tty3215_flush_buffer(struct tty_struct *tty)
  927. {
  928. struct raw3215_info *raw;
  929. raw = (struct raw3215_info *) tty->driver_data;
  930. raw3215_flush_buffer(raw);
  931. tty_wakeup(tty);
  932. }
  933. /*
  934. * Disable reading from a 3215 tty
  935. */
  936. static void tty3215_throttle(struct tty_struct * tty)
  937. {
  938. struct raw3215_info *raw;
  939. raw = (struct raw3215_info *) tty->driver_data;
  940. raw->flags |= RAW3215_THROTTLED;
  941. }
  942. /*
  943. * Enable reading from a 3215 tty
  944. */
  945. static void tty3215_unthrottle(struct tty_struct * tty)
  946. {
  947. struct raw3215_info *raw;
  948. unsigned long flags;
  949. raw = (struct raw3215_info *) tty->driver_data;
  950. if (raw->flags & RAW3215_THROTTLED) {
  951. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  952. raw->flags &= ~RAW3215_THROTTLED;
  953. raw3215_try_io(raw);
  954. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  955. }
  956. }
  957. /*
  958. * Disable writing to a 3215 tty
  959. */
  960. static void tty3215_stop(struct tty_struct *tty)
  961. {
  962. struct raw3215_info *raw;
  963. raw = (struct raw3215_info *) tty->driver_data;
  964. raw->flags |= RAW3215_STOPPED;
  965. }
  966. /*
  967. * Enable writing to a 3215 tty
  968. */
  969. static void tty3215_start(struct tty_struct *tty)
  970. {
  971. struct raw3215_info *raw;
  972. unsigned long flags;
  973. raw = (struct raw3215_info *) tty->driver_data;
  974. if (raw->flags & RAW3215_STOPPED) {
  975. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  976. raw->flags &= ~RAW3215_STOPPED;
  977. raw3215_try_io(raw);
  978. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  979. }
  980. }
  981. static const struct tty_operations tty3215_ops = {
  982. .open = tty3215_open,
  983. .close = tty3215_close,
  984. .write = tty3215_write,
  985. .put_char = tty3215_put_char,
  986. .flush_chars = tty3215_flush_chars,
  987. .write_room = tty3215_write_room,
  988. .chars_in_buffer = tty3215_chars_in_buffer,
  989. .flush_buffer = tty3215_flush_buffer,
  990. .throttle = tty3215_throttle,
  991. .unthrottle = tty3215_unthrottle,
  992. .stop = tty3215_stop,
  993. .start = tty3215_start,
  994. };
  995. /*
  996. * 3215 tty registration code called from tty_init().
  997. * Most kernel services (incl. kmalloc) are available at this poimt.
  998. */
  999. static int __init tty3215_init(void)
  1000. {
  1001. struct tty_driver *driver;
  1002. int ret;
  1003. if (!CONSOLE_IS_3215)
  1004. return 0;
  1005. driver = alloc_tty_driver(NR_3215);
  1006. if (!driver)
  1007. return -ENOMEM;
  1008. ret = ccw_driver_register(&raw3215_ccw_driver);
  1009. if (ret) {
  1010. put_tty_driver(driver);
  1011. return ret;
  1012. }
  1013. /*
  1014. * Initialize the tty_driver structure
  1015. * Entries in tty3215_driver that are NOT initialized:
  1016. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1017. */
  1018. driver->owner = THIS_MODULE;
  1019. driver->driver_name = "tty3215";
  1020. driver->name = "ttyS";
  1021. driver->major = TTY_MAJOR;
  1022. driver->minor_start = 64;
  1023. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1024. driver->subtype = SYSTEM_TYPE_TTY;
  1025. driver->init_termios = tty_std_termios;
  1026. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1027. driver->init_termios.c_oflag = ONLCR | XTABS;
  1028. driver->init_termios.c_lflag = ISIG;
  1029. driver->flags = TTY_DRIVER_REAL_RAW;
  1030. tty_set_operations(driver, &tty3215_ops);
  1031. ret = tty_register_driver(driver);
  1032. if (ret) {
  1033. put_tty_driver(driver);
  1034. return ret;
  1035. }
  1036. tty3215_driver = driver;
  1037. return 0;
  1038. }
  1039. static void __exit tty3215_exit(void)
  1040. {
  1041. tty_unregister_driver(tty3215_driver);
  1042. put_tty_driver(tty3215_driver);
  1043. ccw_driver_unregister(&raw3215_ccw_driver);
  1044. }
  1045. module_init(tty3215_init);
  1046. module_exit(tty3215_exit);