con3215.c 30 KB

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