con3215.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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/module.h>
  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/reboot.h>
  22. #include <linux/serial.h> /* ASYNC_* flags */
  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_WORKING 4 /* set if a request is being worked on */
  45. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  46. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  47. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  48. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  49. #define TAB_STOP_SIZE 8 /* tab stop size */
  50. /*
  51. * Request types for a 3215 device
  52. */
  53. enum raw3215_type {
  54. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  55. };
  56. /*
  57. * Request structure for a 3215 device
  58. */
  59. struct raw3215_req {
  60. enum raw3215_type type; /* type of the request */
  61. int start, len; /* start index & len in output buffer */
  62. int delayable; /* indication to wait for more data */
  63. int residual; /* residual count for read request */
  64. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  65. struct raw3215_info *info; /* pointer to main structure */
  66. struct raw3215_req *next; /* pointer to next request */
  67. } __attribute__ ((aligned(8)));
  68. struct raw3215_info {
  69. struct tty_port port;
  70. struct ccw_device *cdev; /* device for tty driver */
  71. spinlock_t *lock; /* pointer to irq lock */
  72. int flags; /* state flags */
  73. char *buffer; /* pointer to output buffer */
  74. char *inbuf; /* pointer to input buffer */
  75. int head; /* first free byte in output buffer */
  76. int count; /* number of bytes in output buffer */
  77. int written; /* number of bytes in write requests */
  78. struct raw3215_req *queued_read; /* pointer to queued read requests */
  79. struct raw3215_req *queued_write;/* pointer to queued write requests */
  80. struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
  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 spinlock_t 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(unsigned long __data)
  261. {
  262. struct raw3215_info *raw = (struct raw3215_info *) __data;
  263. unsigned long flags;
  264. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  265. raw->flags &= ~RAW3215_TIMER_RUNS;
  266. if (!(raw->port.flags & ASYNC_SUSPENDED)) {
  267. raw3215_mk_write_req(raw);
  268. raw3215_start_io(raw);
  269. if ((raw->queued_read || raw->queued_write) &&
  270. !(raw->flags & RAW3215_WORKING) &&
  271. !(raw->flags & RAW3215_TIMER_RUNS)) {
  272. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  273. add_timer(&raw->timer);
  274. raw->flags |= RAW3215_TIMER_RUNS;
  275. }
  276. }
  277. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  278. }
  279. /*
  280. * Function to conditionally start an IO. A read is started immediately,
  281. * a write is only started immediately if the flush flag is on or the
  282. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  283. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  284. */
  285. static inline void raw3215_try_io(struct raw3215_info *raw)
  286. {
  287. if (!(raw->port.flags & ASYNC_INITIALIZED) ||
  288. (raw->port.flags & ASYNC_SUSPENDED))
  289. return;
  290. if (raw->queued_read != NULL)
  291. raw3215_start_io(raw);
  292. else if (raw->queued_write != NULL) {
  293. if ((raw->queued_write->delayable == 0) ||
  294. (raw->flags & RAW3215_FLUSHING)) {
  295. /* execute write requests bigger than minimum size */
  296. raw3215_start_io(raw);
  297. }
  298. }
  299. if ((raw->queued_read || raw->queued_write) &&
  300. !(raw->flags & RAW3215_WORKING) &&
  301. !(raw->flags & RAW3215_TIMER_RUNS)) {
  302. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  303. add_timer(&raw->timer);
  304. raw->flags |= RAW3215_TIMER_RUNS;
  305. }
  306. }
  307. /*
  308. * Call tty_wakeup from tasklet context
  309. */
  310. static void raw3215_wakeup(unsigned long data)
  311. {
  312. struct raw3215_info *raw = (struct raw3215_info *) data;
  313. struct tty_struct *tty;
  314. tty = tty_port_tty_get(&raw->port);
  315. if (tty) {
  316. tty_wakeup(tty);
  317. tty_kref_put(tty);
  318. }
  319. }
  320. /*
  321. * Try to start the next IO and wake up processes waiting on the tty.
  322. */
  323. static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
  324. {
  325. raw3215_mk_write_req(raw);
  326. raw3215_try_io(raw);
  327. if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
  328. tasklet_schedule(&raw->tlet);
  329. }
  330. /*
  331. * Interrupt routine, called from common io layer
  332. */
  333. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  334. struct irb *irb)
  335. {
  336. struct raw3215_info *raw;
  337. struct raw3215_req *req;
  338. struct tty_struct *tty;
  339. int cstat, dstat;
  340. int count;
  341. raw = dev_get_drvdata(&cdev->dev);
  342. req = (struct raw3215_req *) intparm;
  343. tty = tty_port_tty_get(&raw->port);
  344. cstat = irb->scsw.cmd.cstat;
  345. dstat = irb->scsw.cmd.dstat;
  346. if (cstat != 0)
  347. raw3215_next_io(raw, tty);
  348. if (dstat & 0x01) { /* we got a unit exception */
  349. dstat &= ~0x01; /* we can ignore it */
  350. }
  351. switch (dstat) {
  352. case 0x80:
  353. if (cstat != 0)
  354. break;
  355. /* Attention interrupt, someone hit the enter key */
  356. raw3215_mk_read_req(raw);
  357. raw3215_next_io(raw, tty);
  358. break;
  359. case 0x08:
  360. case 0x0C:
  361. /* Channel end interrupt. */
  362. if ((raw = req->info) == NULL)
  363. goto put_tty; /* That shouldn't happen ... */
  364. if (req->type == RAW3215_READ) {
  365. /* store residual count, then wait for device end */
  366. req->residual = irb->scsw.cmd.count;
  367. }
  368. if (dstat == 0x08)
  369. break;
  370. case 0x04:
  371. /* Device end interrupt. */
  372. if ((raw = req->info) == NULL)
  373. goto put_tty; /* That shouldn't happen ... */
  374. if (req->type == RAW3215_READ && tty != NULL) {
  375. unsigned int cchar;
  376. count = 160 - req->residual;
  377. EBCASC(raw->inbuf, count);
  378. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  379. switch (cchar & CTRLCHAR_MASK) {
  380. case CTRLCHAR_SYSRQ:
  381. break;
  382. case CTRLCHAR_CTRL:
  383. tty_insert_flip_char(&raw->port, cchar,
  384. TTY_NORMAL);
  385. tty_flip_buffer_push(&raw->port);
  386. break;
  387. case CTRLCHAR_NONE:
  388. if (count < 2 ||
  389. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  390. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  391. /* add the auto \n */
  392. raw->inbuf[count] = '\n';
  393. count++;
  394. } else
  395. count -= 2;
  396. tty_insert_flip_string(&raw->port, raw->inbuf,
  397. count);
  398. tty_flip_buffer_push(&raw->port);
  399. break;
  400. }
  401. } else if (req->type == RAW3215_WRITE) {
  402. raw->count -= req->len;
  403. raw->written -= req->len;
  404. }
  405. raw->flags &= ~RAW3215_WORKING;
  406. raw3215_free_req(req);
  407. /* check for empty wait */
  408. if (waitqueue_active(&raw->empty_wait) &&
  409. raw->queued_write == NULL &&
  410. raw->queued_read == NULL) {
  411. wake_up_interruptible(&raw->empty_wait);
  412. }
  413. raw3215_next_io(raw, tty);
  414. break;
  415. default:
  416. /* Strange interrupt, I'll do my best to clean up */
  417. if (req != NULL && req->type != RAW3215_FREE) {
  418. if (req->type == RAW3215_WRITE) {
  419. raw->count -= req->len;
  420. raw->written -= req->len;
  421. }
  422. raw->flags &= ~RAW3215_WORKING;
  423. raw3215_free_req(req);
  424. }
  425. raw3215_next_io(raw, tty);
  426. }
  427. put_tty:
  428. tty_kref_put(tty);
  429. }
  430. /*
  431. * Drop the oldest line from the output buffer.
  432. */
  433. static void raw3215_drop_line(struct raw3215_info *raw)
  434. {
  435. int ix;
  436. char ch;
  437. BUG_ON(raw->written != 0);
  438. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  439. while (raw->count > 0) {
  440. ch = raw->buffer[ix];
  441. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  442. raw->count--;
  443. if (ch == 0x15)
  444. break;
  445. }
  446. raw->head = ix;
  447. }
  448. /*
  449. * Wait until length bytes are available int the output buffer.
  450. * Has to be called with the s390irq lock held. Can be called
  451. * disabled.
  452. */
  453. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  454. {
  455. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  456. /* While console is frozen for suspend we have no other
  457. * choice but to drop message from the buffer to make
  458. * room for even more messages. */
  459. if (raw->port.flags & ASYNC_SUSPENDED) {
  460. raw3215_drop_line(raw);
  461. continue;
  462. }
  463. /* there might be a request pending */
  464. raw->flags |= RAW3215_FLUSHING;
  465. raw3215_mk_write_req(raw);
  466. raw3215_try_io(raw);
  467. raw->flags &= ~RAW3215_FLUSHING;
  468. #ifdef CONFIG_TN3215_CONSOLE
  469. ccw_device_wait_idle(raw->cdev);
  470. #endif
  471. /* Enough room freed up ? */
  472. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  473. break;
  474. /* there might be another cpu waiting for the lock */
  475. spin_unlock(get_ccwdev_lock(raw->cdev));
  476. udelay(100);
  477. spin_lock(get_ccwdev_lock(raw->cdev));
  478. }
  479. }
  480. /*
  481. * String write routine for 3215 devices
  482. */
  483. static void raw3215_write(struct raw3215_info *raw, const char *str,
  484. unsigned int length)
  485. {
  486. unsigned long flags;
  487. int c, count;
  488. while (length > 0) {
  489. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  490. count = (length > RAW3215_BUFFER_SIZE) ?
  491. RAW3215_BUFFER_SIZE : length;
  492. length -= count;
  493. raw3215_make_room(raw, count);
  494. /* copy string to output buffer and convert it to EBCDIC */
  495. while (1) {
  496. c = min_t(int, count,
  497. min(RAW3215_BUFFER_SIZE - raw->count,
  498. RAW3215_BUFFER_SIZE - raw->head));
  499. if (c <= 0)
  500. break;
  501. memcpy(raw->buffer + raw->head, str, c);
  502. ASCEBC(raw->buffer + raw->head, c);
  503. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  504. raw->count += c;
  505. raw->line_pos += c;
  506. str += c;
  507. count -= c;
  508. }
  509. if (!(raw->flags & RAW3215_WORKING)) {
  510. raw3215_mk_write_req(raw);
  511. /* start or queue request */
  512. raw3215_try_io(raw);
  513. }
  514. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  515. }
  516. }
  517. /*
  518. * Put character routine for 3215 devices
  519. */
  520. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  521. {
  522. unsigned long flags;
  523. unsigned int length, i;
  524. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  525. if (ch == '\t') {
  526. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  527. raw->line_pos += length;
  528. ch = ' ';
  529. } else if (ch == '\n') {
  530. length = 1;
  531. raw->line_pos = 0;
  532. } else {
  533. length = 1;
  534. raw->line_pos++;
  535. }
  536. raw3215_make_room(raw, length);
  537. for (i = 0; i < length; i++) {
  538. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  539. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  540. raw->count++;
  541. }
  542. if (!(raw->flags & RAW3215_WORKING)) {
  543. raw3215_mk_write_req(raw);
  544. /* start or queue request */
  545. raw3215_try_io(raw);
  546. }
  547. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  548. }
  549. /*
  550. * Flush routine, it simply sets the flush flag and tries to start
  551. * pending IO.
  552. */
  553. static void raw3215_flush_buffer(struct raw3215_info *raw)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  557. if (raw->count > 0) {
  558. raw->flags |= RAW3215_FLUSHING;
  559. raw3215_try_io(raw);
  560. raw->flags &= ~RAW3215_FLUSHING;
  561. }
  562. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  563. }
  564. /*
  565. * Fire up a 3215 device.
  566. */
  567. static int raw3215_startup(struct raw3215_info *raw)
  568. {
  569. unsigned long flags;
  570. if (raw->port.flags & ASYNC_INITIALIZED)
  571. return 0;
  572. raw->line_pos = 0;
  573. raw->port.flags |= ASYNC_INITIALIZED;
  574. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  575. raw3215_try_io(raw);
  576. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  577. return 0;
  578. }
  579. /*
  580. * Shutdown a 3215 device.
  581. */
  582. static void raw3215_shutdown(struct raw3215_info *raw)
  583. {
  584. DECLARE_WAITQUEUE(wait, current);
  585. unsigned long flags;
  586. if (!(raw->port.flags & ASYNC_INITIALIZED) ||
  587. (raw->flags & RAW3215_FIXED))
  588. return;
  589. /* Wait for outstanding requests, then free irq */
  590. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  591. if ((raw->flags & RAW3215_WORKING) ||
  592. raw->queued_write != NULL ||
  593. raw->queued_read != NULL) {
  594. raw->port.flags |= ASYNC_CLOSING;
  595. add_wait_queue(&raw->empty_wait, &wait);
  596. set_current_state(TASK_INTERRUPTIBLE);
  597. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  598. schedule();
  599. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  600. remove_wait_queue(&raw->empty_wait, &wait);
  601. set_current_state(TASK_RUNNING);
  602. raw->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING);
  603. }
  604. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  605. }
  606. static struct raw3215_info *raw3215_alloc_info(void)
  607. {
  608. struct raw3215_info *info;
  609. info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  610. if (!info)
  611. return NULL;
  612. info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  613. info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  614. if (!info->buffer || !info->inbuf) {
  615. kfree(info->inbuf);
  616. kfree(info->buffer);
  617. kfree(info);
  618. return NULL;
  619. }
  620. setup_timer(&info->timer, raw3215_timeout, (unsigned long)info);
  621. init_waitqueue_head(&info->empty_wait);
  622. tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
  623. tty_port_init(&info->port);
  624. return info;
  625. }
  626. static void raw3215_free_info(struct raw3215_info *raw)
  627. {
  628. kfree(raw->inbuf);
  629. kfree(raw->buffer);
  630. tty_port_destroy(&raw->port);
  631. kfree(raw);
  632. }
  633. static int raw3215_probe (struct ccw_device *cdev)
  634. {
  635. struct raw3215_info *raw;
  636. int line;
  637. /* Console is special. */
  638. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  639. return 0;
  640. raw = raw3215_alloc_info();
  641. if (raw == NULL)
  642. return -ENOMEM;
  643. raw->cdev = cdev;
  644. dev_set_drvdata(&cdev->dev, raw);
  645. cdev->handler = raw3215_irq;
  646. spin_lock(&raw3215_device_lock);
  647. for (line = 0; line < NR_3215; line++) {
  648. if (!raw3215[line]) {
  649. raw3215[line] = raw;
  650. break;
  651. }
  652. }
  653. spin_unlock(&raw3215_device_lock);
  654. if (line == NR_3215) {
  655. raw3215_free_info(raw);
  656. return -ENODEV;
  657. }
  658. return 0;
  659. }
  660. static void raw3215_remove (struct ccw_device *cdev)
  661. {
  662. struct raw3215_info *raw;
  663. unsigned int line;
  664. ccw_device_set_offline(cdev);
  665. raw = dev_get_drvdata(&cdev->dev);
  666. if (raw) {
  667. spin_lock(&raw3215_device_lock);
  668. for (line = 0; line < NR_3215; line++)
  669. if (raw3215[line] == raw)
  670. break;
  671. raw3215[line] = NULL;
  672. spin_unlock(&raw3215_device_lock);
  673. dev_set_drvdata(&cdev->dev, NULL);
  674. raw3215_free_info(raw);
  675. }
  676. }
  677. static int raw3215_set_online (struct ccw_device *cdev)
  678. {
  679. struct raw3215_info *raw;
  680. raw = dev_get_drvdata(&cdev->dev);
  681. if (!raw)
  682. return -ENODEV;
  683. return raw3215_startup(raw);
  684. }
  685. static int raw3215_set_offline (struct ccw_device *cdev)
  686. {
  687. struct raw3215_info *raw;
  688. raw = dev_get_drvdata(&cdev->dev);
  689. if (!raw)
  690. return -ENODEV;
  691. raw3215_shutdown(raw);
  692. return 0;
  693. }
  694. static int raw3215_pm_stop(struct ccw_device *cdev)
  695. {
  696. struct raw3215_info *raw;
  697. unsigned long flags;
  698. /* Empty the output buffer, then prevent new I/O. */
  699. raw = dev_get_drvdata(&cdev->dev);
  700. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  701. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  702. raw->port.flags |= ASYNC_SUSPENDED;
  703. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  704. return 0;
  705. }
  706. static int raw3215_pm_start(struct ccw_device *cdev)
  707. {
  708. struct raw3215_info *raw;
  709. unsigned long flags;
  710. /* Allow I/O again and flush output buffer. */
  711. raw = dev_get_drvdata(&cdev->dev);
  712. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  713. raw->port.flags &= ~ASYNC_SUSPENDED;
  714. raw->flags |= RAW3215_FLUSHING;
  715. raw3215_try_io(raw);
  716. raw->flags &= ~RAW3215_FLUSHING;
  717. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  718. return 0;
  719. }
  720. static struct ccw_device_id raw3215_id[] = {
  721. { CCW_DEVICE(0x3215, 0) },
  722. { /* end of list */ },
  723. };
  724. static struct ccw_driver raw3215_ccw_driver = {
  725. .driver = {
  726. .name = "3215",
  727. .owner = THIS_MODULE,
  728. },
  729. .ids = raw3215_id,
  730. .probe = &raw3215_probe,
  731. .remove = &raw3215_remove,
  732. .set_online = &raw3215_set_online,
  733. .set_offline = &raw3215_set_offline,
  734. .freeze = &raw3215_pm_stop,
  735. .thaw = &raw3215_pm_start,
  736. .restore = &raw3215_pm_start,
  737. .int_class = IRQIO_C15,
  738. };
  739. #ifdef CONFIG_TN3215_CONSOLE
  740. /*
  741. * Write a string to the 3215 console
  742. */
  743. static void con3215_write(struct console *co, const char *str,
  744. unsigned int count)
  745. {
  746. struct raw3215_info *raw;
  747. int i;
  748. if (count <= 0)
  749. return;
  750. raw = raw3215[0]; /* console 3215 is the first one */
  751. while (count > 0) {
  752. for (i = 0; i < count; i++)
  753. if (str[i] == '\t' || str[i] == '\n')
  754. break;
  755. raw3215_write(raw, str, i);
  756. count -= i;
  757. str += i;
  758. if (count > 0) {
  759. raw3215_putchar(raw, *str);
  760. count--;
  761. str++;
  762. }
  763. }
  764. }
  765. static struct tty_driver *con3215_device(struct console *c, int *index)
  766. {
  767. *index = c->index;
  768. return tty3215_driver;
  769. }
  770. /*
  771. * panic() calls con3215_flush through a panic_notifier
  772. * before the system enters a disabled, endless loop.
  773. */
  774. static void con3215_flush(void)
  775. {
  776. struct raw3215_info *raw;
  777. unsigned long flags;
  778. raw = raw3215[0]; /* console 3215 is the first one */
  779. if (raw->port.flags & ASYNC_SUSPENDED)
  780. /* The console is still frozen for suspend. */
  781. if (ccw_device_force_console(raw->cdev))
  782. /* Forcing didn't work, no panic message .. */
  783. return;
  784. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  785. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  786. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  787. }
  788. static int con3215_notify(struct notifier_block *self,
  789. unsigned long event, void *data)
  790. {
  791. con3215_flush();
  792. return NOTIFY_OK;
  793. }
  794. static struct notifier_block on_panic_nb = {
  795. .notifier_call = con3215_notify,
  796. .priority = 0,
  797. };
  798. static struct notifier_block on_reboot_nb = {
  799. .notifier_call = con3215_notify,
  800. .priority = 0,
  801. };
  802. /*
  803. * The console structure for the 3215 console
  804. */
  805. static struct console con3215 = {
  806. .name = "ttyS",
  807. .write = con3215_write,
  808. .device = con3215_device,
  809. .flags = CON_PRINTBUFFER,
  810. };
  811. /*
  812. * 3215 console initialization code called from console_init().
  813. */
  814. static int __init con3215_init(void)
  815. {
  816. struct ccw_device *cdev;
  817. struct raw3215_info *raw;
  818. struct raw3215_req *req;
  819. int i;
  820. /* Check if 3215 is to be the console */
  821. if (!CONSOLE_IS_3215)
  822. return -ENODEV;
  823. /* Set the console mode for VM */
  824. if (MACHINE_IS_VM) {
  825. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  826. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  827. }
  828. /* allocate 3215 request structures */
  829. raw3215_freelist = NULL;
  830. spin_lock_init(&raw3215_freelist_lock);
  831. for (i = 0; i < NR_3215_REQ; i++) {
  832. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  833. req->next = raw3215_freelist;
  834. raw3215_freelist = req;
  835. }
  836. cdev = ccw_device_create_console(&raw3215_ccw_driver);
  837. if (IS_ERR(cdev))
  838. return -ENODEV;
  839. raw3215[0] = raw = raw3215_alloc_info();
  840. raw->cdev = cdev;
  841. dev_set_drvdata(&cdev->dev, raw);
  842. cdev->handler = raw3215_irq;
  843. raw->flags |= RAW3215_FIXED;
  844. if (ccw_device_enable_console(cdev)) {
  845. ccw_device_destroy_console(cdev);
  846. raw3215_free_info(raw);
  847. raw3215[0] = NULL;
  848. return -ENODEV;
  849. }
  850. /* Request the console irq */
  851. if (raw3215_startup(raw) != 0) {
  852. raw3215_free_info(raw);
  853. raw3215[0] = NULL;
  854. return -ENODEV;
  855. }
  856. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  857. register_reboot_notifier(&on_reboot_nb);
  858. register_console(&con3215);
  859. return 0;
  860. }
  861. console_initcall(con3215_init);
  862. #endif
  863. static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
  864. {
  865. struct raw3215_info *raw;
  866. raw = raw3215[tty->index];
  867. if (raw == NULL)
  868. return -ENODEV;
  869. tty->driver_data = raw;
  870. return tty_port_install(&raw->port, driver, tty);
  871. }
  872. /*
  873. * tty3215_open
  874. *
  875. * This routine is called whenever a 3215 tty is opened.
  876. */
  877. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  878. {
  879. struct raw3215_info *raw = tty->driver_data;
  880. int retval;
  881. tty_port_tty_set(&raw->port, tty);
  882. raw->port.low_latency = 0; /* don't use bottom half for pushing chars */
  883. /*
  884. * Start up 3215 device
  885. */
  886. retval = raw3215_startup(raw);
  887. if (retval)
  888. return retval;
  889. return 0;
  890. }
  891. /*
  892. * tty3215_close()
  893. *
  894. * This routine is called when the 3215 tty is closed. We wait
  895. * for the remaining request to be completed. Then we clean up.
  896. */
  897. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  898. {
  899. struct raw3215_info *raw;
  900. raw = (struct raw3215_info *) tty->driver_data;
  901. if (raw == NULL || tty->count > 1)
  902. return;
  903. tty->closing = 1;
  904. /* Shutdown the terminal */
  905. raw3215_shutdown(raw);
  906. tasklet_kill(&raw->tlet);
  907. tty->closing = 0;
  908. tty_port_tty_set(&raw->port, NULL);
  909. }
  910. /*
  911. * Returns the amount of free space in the output buffer.
  912. */
  913. static int tty3215_write_room(struct tty_struct *tty)
  914. {
  915. struct raw3215_info *raw;
  916. raw = (struct raw3215_info *) tty->driver_data;
  917. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  918. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  919. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  920. else
  921. return 0;
  922. }
  923. /*
  924. * String write routine for 3215 ttys
  925. */
  926. static int tty3215_write(struct tty_struct * tty,
  927. const unsigned char *buf, int count)
  928. {
  929. struct raw3215_info *raw;
  930. int i, written;
  931. if (!tty)
  932. return 0;
  933. raw = (struct raw3215_info *) tty->driver_data;
  934. written = count;
  935. while (count > 0) {
  936. for (i = 0; i < count; i++)
  937. if (buf[i] == '\t' || buf[i] == '\n')
  938. break;
  939. raw3215_write(raw, buf, i);
  940. count -= i;
  941. buf += i;
  942. if (count > 0) {
  943. raw3215_putchar(raw, *buf);
  944. count--;
  945. buf++;
  946. }
  947. }
  948. return written;
  949. }
  950. /*
  951. * Put character routine for 3215 ttys
  952. */
  953. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  954. {
  955. struct raw3215_info *raw;
  956. if (!tty)
  957. return 0;
  958. raw = (struct raw3215_info *) tty->driver_data;
  959. raw3215_putchar(raw, ch);
  960. return 1;
  961. }
  962. static void tty3215_flush_chars(struct tty_struct *tty)
  963. {
  964. }
  965. /*
  966. * Returns the number of characters in the output buffer
  967. */
  968. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  969. {
  970. struct raw3215_info *raw;
  971. raw = (struct raw3215_info *) tty->driver_data;
  972. return raw->count;
  973. }
  974. static void tty3215_flush_buffer(struct tty_struct *tty)
  975. {
  976. struct raw3215_info *raw;
  977. raw = (struct raw3215_info *) tty->driver_data;
  978. raw3215_flush_buffer(raw);
  979. tty_wakeup(tty);
  980. }
  981. /*
  982. * Disable reading from a 3215 tty
  983. */
  984. static void tty3215_throttle(struct tty_struct * tty)
  985. {
  986. struct raw3215_info *raw;
  987. raw = (struct raw3215_info *) tty->driver_data;
  988. raw->flags |= RAW3215_THROTTLED;
  989. }
  990. /*
  991. * Enable reading from a 3215 tty
  992. */
  993. static void tty3215_unthrottle(struct tty_struct * tty)
  994. {
  995. struct raw3215_info *raw;
  996. unsigned long flags;
  997. raw = (struct raw3215_info *) tty->driver_data;
  998. if (raw->flags & RAW3215_THROTTLED) {
  999. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  1000. raw->flags &= ~RAW3215_THROTTLED;
  1001. raw3215_try_io(raw);
  1002. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1003. }
  1004. }
  1005. /*
  1006. * Disable writing to a 3215 tty
  1007. */
  1008. static void tty3215_stop(struct tty_struct *tty)
  1009. {
  1010. struct raw3215_info *raw;
  1011. raw = (struct raw3215_info *) tty->driver_data;
  1012. raw->flags |= RAW3215_STOPPED;
  1013. }
  1014. /*
  1015. * Enable writing to a 3215 tty
  1016. */
  1017. static void tty3215_start(struct tty_struct *tty)
  1018. {
  1019. struct raw3215_info *raw;
  1020. unsigned long flags;
  1021. raw = (struct raw3215_info *) tty->driver_data;
  1022. if (raw->flags & RAW3215_STOPPED) {
  1023. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  1024. raw->flags &= ~RAW3215_STOPPED;
  1025. raw3215_try_io(raw);
  1026. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  1027. }
  1028. }
  1029. static const struct tty_operations tty3215_ops = {
  1030. .install = tty3215_install,
  1031. .open = tty3215_open,
  1032. .close = tty3215_close,
  1033. .write = tty3215_write,
  1034. .put_char = tty3215_put_char,
  1035. .flush_chars = tty3215_flush_chars,
  1036. .write_room = tty3215_write_room,
  1037. .chars_in_buffer = tty3215_chars_in_buffer,
  1038. .flush_buffer = tty3215_flush_buffer,
  1039. .throttle = tty3215_throttle,
  1040. .unthrottle = tty3215_unthrottle,
  1041. .stop = tty3215_stop,
  1042. .start = tty3215_start,
  1043. };
  1044. /*
  1045. * 3215 tty registration code called from tty_init().
  1046. * Most kernel services (incl. kmalloc) are available at this poimt.
  1047. */
  1048. static int __init tty3215_init(void)
  1049. {
  1050. struct tty_driver *driver;
  1051. int ret;
  1052. if (!CONSOLE_IS_3215)
  1053. return 0;
  1054. driver = alloc_tty_driver(NR_3215);
  1055. if (!driver)
  1056. return -ENOMEM;
  1057. ret = ccw_driver_register(&raw3215_ccw_driver);
  1058. if (ret) {
  1059. put_tty_driver(driver);
  1060. return ret;
  1061. }
  1062. /*
  1063. * Initialize the tty_driver structure
  1064. * Entries in tty3215_driver that are NOT initialized:
  1065. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1066. */
  1067. driver->driver_name = "tty3215";
  1068. driver->name = "ttyS";
  1069. driver->major = TTY_MAJOR;
  1070. driver->minor_start = 64;
  1071. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1072. driver->subtype = SYSTEM_TYPE_TTY;
  1073. driver->init_termios = tty_std_termios;
  1074. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1075. driver->init_termios.c_oflag = ONLCR;
  1076. driver->init_termios.c_lflag = ISIG;
  1077. driver->flags = TTY_DRIVER_REAL_RAW;
  1078. tty_set_operations(driver, &tty3215_ops);
  1079. ret = tty_register_driver(driver);
  1080. if (ret) {
  1081. put_tty_driver(driver);
  1082. return ret;
  1083. }
  1084. tty3215_driver = driver;
  1085. return 0;
  1086. }
  1087. static void __exit tty3215_exit(void)
  1088. {
  1089. tty_unregister_driver(tty3215_driver);
  1090. put_tty_driver(tty3215_driver);
  1091. ccw_driver_unregister(&raw3215_ccw_driver);
  1092. }
  1093. module_init(tty3215_init);
  1094. module_exit(tty3215_exit);