con3215.c 28 KB

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