raw3270.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /*
  2. * drivers/s390/char/raw3270.c
  3. * IBM/3270 Driver - core functions.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  9. */
  10. #include <linux/config.h>
  11. #include <linux/bootmem.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/wait.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/ebcdic.h>
  23. #include "raw3270.h"
  24. #include <linux/major.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/device.h>
  27. #include <linux/mutex.h>
  28. struct class *class3270;
  29. /* The main 3270 data structure. */
  30. struct raw3270 {
  31. struct list_head list;
  32. struct ccw_device *cdev;
  33. int minor;
  34. short model, rows, cols;
  35. unsigned long flags;
  36. struct list_head req_queue; /* Request queue. */
  37. struct list_head view_list; /* List of available views. */
  38. struct raw3270_view *view; /* Active view. */
  39. struct timer_list timer; /* Device timer. */
  40. unsigned char *ascebc; /* ascii -> ebcdic table */
  41. struct class_device *clttydev; /* 3270-class tty device ptr */
  42. struct class_device *cltubdev; /* 3270-class tub device ptr */
  43. };
  44. /* raw3270->flags */
  45. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  46. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  47. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  48. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  49. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  50. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  51. static DEFINE_MUTEX(raw3270_mutex);
  52. /* List of 3270 devices. */
  53. static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
  54. /*
  55. * Flag to indicate if the driver has been registered. Some operations
  56. * like waiting for the end of i/o need to be done differently as long
  57. * as the kernel is still starting up (console support).
  58. */
  59. static int raw3270_registered;
  60. /* Module parameters */
  61. static int tubxcorrect = 0;
  62. module_param(tubxcorrect, bool, 0);
  63. /*
  64. * Wait queue for device init/delete, view delete.
  65. */
  66. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  67. /*
  68. * Encode array for 12 bit 3270 addresses.
  69. */
  70. unsigned char raw3270_ebcgraf[64] = {
  71. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  72. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  73. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  74. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  75. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  76. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  77. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  78. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  79. };
  80. void
  81. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  82. {
  83. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  84. cp[0] = (addr >> 8) & 0x3f;
  85. cp[1] = addr & 0xff;
  86. } else {
  87. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  88. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  89. }
  90. }
  91. /*
  92. * Allocate a new 3270 ccw request
  93. */
  94. struct raw3270_request *
  95. raw3270_request_alloc(size_t size)
  96. {
  97. struct raw3270_request *rq;
  98. /* Allocate request structure */
  99. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  100. if (!rq)
  101. return ERR_PTR(-ENOMEM);
  102. /* alloc output buffer. */
  103. if (size > 0) {
  104. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  105. if (!rq->buffer) {
  106. kfree(rq);
  107. return ERR_PTR(-ENOMEM);
  108. }
  109. }
  110. rq->size = size;
  111. INIT_LIST_HEAD(&rq->list);
  112. /*
  113. * Setup ccw.
  114. */
  115. rq->ccw.cda = __pa(rq->buffer);
  116. rq->ccw.flags = CCW_FLAG_SLI;
  117. return rq;
  118. }
  119. #ifdef CONFIG_TN3270_CONSOLE
  120. /*
  121. * Allocate a new 3270 ccw request from bootmem. Only works very
  122. * early in the boot process. Only con3270.c should be using this.
  123. */
  124. struct raw3270_request *
  125. raw3270_request_alloc_bootmem(size_t size)
  126. {
  127. struct raw3270_request *rq;
  128. rq = alloc_bootmem_low(sizeof(struct raw3270));
  129. if (!rq)
  130. return ERR_PTR(-ENOMEM);
  131. memset(rq, 0, sizeof(struct raw3270_request));
  132. /* alloc output buffer. */
  133. if (size > 0) {
  134. rq->buffer = alloc_bootmem_low(size);
  135. if (!rq->buffer) {
  136. free_bootmem((unsigned long) rq,
  137. sizeof(struct raw3270));
  138. return ERR_PTR(-ENOMEM);
  139. }
  140. }
  141. rq->size = size;
  142. INIT_LIST_HEAD(&rq->list);
  143. /*
  144. * Setup ccw.
  145. */
  146. rq->ccw.cda = __pa(rq->buffer);
  147. rq->ccw.flags = CCW_FLAG_SLI;
  148. return rq;
  149. }
  150. #endif
  151. /*
  152. * Free 3270 ccw request
  153. */
  154. void
  155. raw3270_request_free (struct raw3270_request *rq)
  156. {
  157. kfree(rq->buffer);
  158. kfree(rq);
  159. }
  160. /*
  161. * Reset request to initial state.
  162. */
  163. void
  164. raw3270_request_reset(struct raw3270_request *rq)
  165. {
  166. BUG_ON(!list_empty(&rq->list));
  167. rq->ccw.cmd_code = 0;
  168. rq->ccw.count = 0;
  169. rq->ccw.cda = __pa(rq->buffer);
  170. rq->ccw.flags = CCW_FLAG_SLI;
  171. rq->rescnt = 0;
  172. rq->rc = 0;
  173. }
  174. /*
  175. * Set command code to ccw of a request.
  176. */
  177. void
  178. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  179. {
  180. rq->ccw.cmd_code = cmd;
  181. }
  182. /*
  183. * Add data fragment to output buffer.
  184. */
  185. int
  186. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  187. {
  188. if (size + rq->ccw.count > rq->size)
  189. return -E2BIG;
  190. memcpy(rq->buffer + rq->ccw.count, data, size);
  191. rq->ccw.count += size;
  192. return 0;
  193. }
  194. /*
  195. * Set address/length pair to ccw of a request.
  196. */
  197. void
  198. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  199. {
  200. rq->ccw.cda = __pa(data);
  201. rq->ccw.count = size;
  202. }
  203. /*
  204. * Set idal buffer to ccw of a request.
  205. */
  206. void
  207. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  208. {
  209. rq->ccw.cda = __pa(ib->data);
  210. rq->ccw.count = ib->size;
  211. rq->ccw.flags |= CCW_FLAG_IDA;
  212. }
  213. /*
  214. * Stop running ccw.
  215. */
  216. static int
  217. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  218. {
  219. int retries;
  220. int rc;
  221. if (raw3270_request_final(rq))
  222. return 0;
  223. /* Check if interrupt has already been processed */
  224. for (retries = 0; retries < 5; retries++) {
  225. if (retries < 2)
  226. rc = ccw_device_halt(rp->cdev, (long) rq);
  227. else
  228. rc = ccw_device_clear(rp->cdev, (long) rq);
  229. if (rc == 0)
  230. break; /* termination successful */
  231. }
  232. return rc;
  233. }
  234. static int
  235. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  236. {
  237. unsigned long flags;
  238. int rc;
  239. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  240. rc = raw3270_halt_io_nolock(rp, rq);
  241. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  242. return rc;
  243. }
  244. /*
  245. * Add the request to the request queue, try to start it if the
  246. * 3270 device is idle. Return without waiting for end of i/o.
  247. */
  248. static int
  249. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  250. struct raw3270_request *rq)
  251. {
  252. rq->view = view;
  253. raw3270_get_view(view);
  254. if (list_empty(&rp->req_queue) &&
  255. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  256. /* No other requests are on the queue. Start this one. */
  257. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  258. (unsigned long) rq, 0, 0);
  259. if (rq->rc) {
  260. raw3270_put_view(view);
  261. return rq->rc;
  262. }
  263. }
  264. list_add_tail(&rq->list, &rp->req_queue);
  265. return 0;
  266. }
  267. int
  268. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  269. {
  270. unsigned long flags;
  271. struct raw3270 *rp;
  272. int rc;
  273. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  274. rp = view->dev;
  275. if (!rp || rp->view != view)
  276. rc = -EACCES;
  277. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  278. rc = -ENODEV;
  279. else
  280. rc = __raw3270_start(rp, view, rq);
  281. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  282. return rc;
  283. }
  284. int
  285. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  286. {
  287. struct raw3270 *rp;
  288. int rc;
  289. rp = view->dev;
  290. if (!rp || rp->view != view)
  291. rc = -EACCES;
  292. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  293. rc = -ENODEV;
  294. else
  295. rc = __raw3270_start(rp, view, rq);
  296. return rc;
  297. }
  298. int
  299. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  300. {
  301. struct raw3270 *rp;
  302. rp = view->dev;
  303. rq->view = view;
  304. raw3270_get_view(view);
  305. list_add_tail(&rq->list, &rp->req_queue);
  306. return 0;
  307. }
  308. /*
  309. * 3270 interrupt routine, called from the ccw_device layer
  310. */
  311. static void
  312. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  313. {
  314. struct raw3270 *rp;
  315. struct raw3270_view *view;
  316. struct raw3270_request *rq;
  317. int rc;
  318. rp = (struct raw3270 *) cdev->dev.driver_data;
  319. if (!rp)
  320. return;
  321. rq = (struct raw3270_request *) intparm;
  322. view = rq ? rq->view : rp->view;
  323. if (IS_ERR(irb))
  324. rc = RAW3270_IO_RETRY;
  325. else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  326. rq->rc = -EIO;
  327. rc = RAW3270_IO_DONE;
  328. } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  329. DEV_STAT_UNIT_EXCEP)) {
  330. /* Handle CE-DE-UE and subsequent UDE */
  331. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  332. rc = RAW3270_IO_BUSY;
  333. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  334. /* Wait for UDE if busy flag is set. */
  335. if (irb->scsw.dstat & DEV_STAT_DEV_END) {
  336. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  337. /* Got it, now retry. */
  338. rc = RAW3270_IO_RETRY;
  339. } else
  340. rc = RAW3270_IO_BUSY;
  341. } else if (view)
  342. rc = view->fn->intv(view, rq, irb);
  343. else
  344. rc = RAW3270_IO_DONE;
  345. switch (rc) {
  346. case RAW3270_IO_DONE:
  347. break;
  348. case RAW3270_IO_BUSY:
  349. /*
  350. * Intervention required by the operator. We have to wait
  351. * for unsolicited device end.
  352. */
  353. return;
  354. case RAW3270_IO_RETRY:
  355. if (!rq)
  356. break;
  357. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  358. (unsigned long) rq, 0, 0);
  359. if (rq->rc == 0)
  360. return; /* Sucessfully restarted. */
  361. break;
  362. case RAW3270_IO_STOP:
  363. if (!rq)
  364. break;
  365. raw3270_halt_io_nolock(rp, rq);
  366. rq->rc = -EIO;
  367. break;
  368. default:
  369. BUG();
  370. }
  371. if (rq) {
  372. BUG_ON(list_empty(&rq->list));
  373. /* The request completed, remove from queue and do callback. */
  374. list_del_init(&rq->list);
  375. if (rq->callback)
  376. rq->callback(rq, rq->callback_data);
  377. /* Do put_device for get_device in raw3270_start. */
  378. raw3270_put_view(view);
  379. }
  380. /*
  381. * Try to start each request on request queue until one is
  382. * started successful.
  383. */
  384. while (!list_empty(&rp->req_queue)) {
  385. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  386. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  387. (unsigned long) rq, 0, 0);
  388. if (rq->rc == 0)
  389. break;
  390. /* Start failed. Remove request and do callback. */
  391. list_del_init(&rq->list);
  392. if (rq->callback)
  393. rq->callback(rq, rq->callback_data);
  394. /* Do put_device for get_device in raw3270_start. */
  395. raw3270_put_view(view);
  396. }
  397. }
  398. /*
  399. * Size sensing.
  400. */
  401. struct raw3270_ua { /* Query Reply structure for Usable Area */
  402. struct { /* Usable Area Query Reply Base */
  403. short l; /* Length of this structured field */
  404. char sfid; /* 0x81 if Query Reply */
  405. char qcode; /* 0x81 if Usable Area */
  406. char flags0;
  407. char flags1;
  408. short w; /* Width of usable area */
  409. short h; /* Heigth of usavle area */
  410. char units; /* 0x00:in; 0x01:mm */
  411. int xr;
  412. int yr;
  413. char aw;
  414. char ah;
  415. short buffsz; /* Character buffer size, bytes */
  416. char xmin;
  417. char ymin;
  418. char xmax;
  419. char ymax;
  420. } __attribute__ ((packed)) uab;
  421. struct { /* Alternate Usable Area Self-Defining Parameter */
  422. char l; /* Length of this Self-Defining Parm */
  423. char sdpid; /* 0x02 if Alternate Usable Area */
  424. char res;
  425. char auaid; /* 0x01 is Id for the A U A */
  426. short wauai; /* Width of AUAi */
  427. short hauai; /* Height of AUAi */
  428. char auaunits; /* 0x00:in, 0x01:mm */
  429. int auaxr;
  430. int auayr;
  431. char awauai;
  432. char ahauai;
  433. } __attribute__ ((packed)) aua;
  434. } __attribute__ ((packed));
  435. static unsigned char raw3270_init_data[256];
  436. static struct raw3270_request raw3270_init_request;
  437. static struct diag210 raw3270_init_diag210;
  438. static DECLARE_MUTEX(raw3270_init_sem);
  439. static int
  440. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  441. struct irb *irb)
  442. {
  443. /*
  444. * Unit-Check Processing:
  445. * Expect Command Reject or Intervention Required.
  446. */
  447. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  448. /* Request finished abnormally. */
  449. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  450. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  451. return RAW3270_IO_BUSY;
  452. }
  453. }
  454. if (rq) {
  455. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  456. if (irb->ecw[0] & SNS0_CMD_REJECT)
  457. rq->rc = -EOPNOTSUPP;
  458. else
  459. rq->rc = -EIO;
  460. } else
  461. /* Request finished normally. Copy residual count. */
  462. rq->rescnt = irb->scsw.count;
  463. }
  464. if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
  465. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  466. wake_up(&raw3270_wait_queue);
  467. }
  468. return RAW3270_IO_DONE;
  469. }
  470. static struct raw3270_fn raw3270_init_fn = {
  471. .intv = raw3270_init_irq
  472. };
  473. static struct raw3270_view raw3270_init_view = {
  474. .fn = &raw3270_init_fn
  475. };
  476. /*
  477. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  478. * Wait for end of request. The request must have been started
  479. * with raw3270_start, rc = 0. The device lock may NOT have been
  480. * released between calling raw3270_start and raw3270_wait.
  481. */
  482. static void
  483. raw3270_wake_init(struct raw3270_request *rq, void *data)
  484. {
  485. wake_up((wait_queue_head_t *) data);
  486. }
  487. /*
  488. * Special wait function that can cope with console initialization.
  489. */
  490. static int
  491. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  492. struct raw3270_request *rq)
  493. {
  494. unsigned long flags;
  495. wait_queue_head_t wq;
  496. int rc;
  497. #ifdef CONFIG_TN3270_CONSOLE
  498. if (raw3270_registered == 0) {
  499. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  500. rq->callback = 0;
  501. rc = __raw3270_start(rp, view, rq);
  502. if (rc == 0)
  503. while (!raw3270_request_final(rq)) {
  504. wait_cons_dev();
  505. barrier();
  506. }
  507. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  508. return rq->rc;
  509. }
  510. #endif
  511. init_waitqueue_head(&wq);
  512. rq->callback = raw3270_wake_init;
  513. rq->callback_data = &wq;
  514. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  515. rc = __raw3270_start(rp, view, rq);
  516. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  517. if (rc)
  518. return rc;
  519. /* Now wait for the completion. */
  520. rc = wait_event_interruptible(wq, raw3270_request_final(rq));
  521. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  522. raw3270_halt_io(view->dev, rq);
  523. /* No wait for the halt to complete. */
  524. wait_event(wq, raw3270_request_final(rq));
  525. return -ERESTARTSYS;
  526. }
  527. return rq->rc;
  528. }
  529. static int
  530. __raw3270_size_device_vm(struct raw3270 *rp)
  531. {
  532. int rc, model;
  533. raw3270_init_diag210.vrdcdvno =
  534. _ccw_device_get_device_number(rp->cdev);
  535. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  536. rc = diag210(&raw3270_init_diag210);
  537. if (rc)
  538. return rc;
  539. model = raw3270_init_diag210.vrdccrmd;
  540. switch (model) {
  541. case 2:
  542. rp->model = model;
  543. rp->rows = 24;
  544. rp->cols = 80;
  545. break;
  546. case 3:
  547. rp->model = model;
  548. rp->rows = 32;
  549. rp->cols = 80;
  550. break;
  551. case 4:
  552. rp->model = model;
  553. rp->rows = 43;
  554. rp->cols = 80;
  555. break;
  556. case 5:
  557. rp->model = model;
  558. rp->rows = 27;
  559. rp->cols = 132;
  560. break;
  561. default:
  562. printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
  563. rc = -EOPNOTSUPP;
  564. break;
  565. }
  566. return rc;
  567. }
  568. static int
  569. __raw3270_size_device(struct raw3270 *rp)
  570. {
  571. static const unsigned char wbuf[] =
  572. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  573. struct raw3270_ua *uap;
  574. unsigned short count;
  575. int rc;
  576. /*
  577. * To determine the size of the 3270 device we need to do:
  578. * 1) send a 'read partition' data stream to the device
  579. * 2) wait for the attn interrupt that preceeds the query reply
  580. * 3) do a read modified to get the query reply
  581. * To make things worse we have to cope with intervention
  582. * required (3270 device switched to 'stand-by') and command
  583. * rejects (old devices that can't do 'read partition').
  584. */
  585. memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
  586. memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
  587. /* Store 'read partition' data stream to raw3270_init_data */
  588. memcpy(raw3270_init_data, wbuf, sizeof(wbuf));
  589. INIT_LIST_HEAD(&raw3270_init_request.list);
  590. raw3270_init_request.ccw.cmd_code = TC_WRITESF;
  591. raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
  592. raw3270_init_request.ccw.count = sizeof(wbuf);
  593. raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
  594. rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
  595. if (rc)
  596. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  597. return rc;
  598. /* Wait for attention interrupt. */
  599. #ifdef CONFIG_TN3270_CONSOLE
  600. if (raw3270_registered == 0) {
  601. unsigned long flags;
  602. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  603. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  604. wait_cons_dev();
  605. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  606. } else
  607. #endif
  608. rc = wait_event_interruptible(raw3270_wait_queue,
  609. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  610. if (rc)
  611. return rc;
  612. /*
  613. * The device accepted the 'read partition' command. Now
  614. * set up a read ccw and issue it.
  615. */
  616. raw3270_init_request.ccw.cmd_code = TC_READMOD;
  617. raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
  618. raw3270_init_request.ccw.count = sizeof(raw3270_init_data);
  619. raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
  620. rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
  621. if (rc)
  622. return rc;
  623. /* Got a Query Reply */
  624. count = sizeof(raw3270_init_data) - raw3270_init_request.rescnt;
  625. uap = (struct raw3270_ua *) (raw3270_init_data + 1);
  626. /* Paranoia check. */
  627. if (raw3270_init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  628. return -EOPNOTSUPP;
  629. /* Copy rows/columns of default Usable Area */
  630. rp->rows = uap->uab.h;
  631. rp->cols = uap->uab.w;
  632. /* Check for 14 bit addressing */
  633. if ((uap->uab.flags0 & 0x0d) == 0x01)
  634. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  635. /* Check for Alternate Usable Area */
  636. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  637. uap->aua.sdpid == 0x02) {
  638. rp->rows = uap->aua.hauai;
  639. rp->cols = uap->aua.wauai;
  640. }
  641. return 0;
  642. }
  643. static int
  644. raw3270_size_device(struct raw3270 *rp)
  645. {
  646. int rc;
  647. down(&raw3270_init_sem);
  648. rp->view = &raw3270_init_view;
  649. raw3270_init_view.dev = rp;
  650. if (MACHINE_IS_VM)
  651. rc = __raw3270_size_device_vm(rp);
  652. else
  653. rc = __raw3270_size_device(rp);
  654. raw3270_init_view.dev = 0;
  655. rp->view = 0;
  656. up(&raw3270_init_sem);
  657. if (rc == 0) { /* Found something. */
  658. /* Try to find a model. */
  659. rp->model = 0;
  660. if (rp->rows == 24 && rp->cols == 80)
  661. rp->model = 2;
  662. if (rp->rows == 32 && rp->cols == 80)
  663. rp->model = 3;
  664. if (rp->rows == 43 && rp->cols == 80)
  665. rp->model = 4;
  666. if (rp->rows == 27 && rp->cols == 132)
  667. rp->model = 5;
  668. } else {
  669. /* Couldn't detect size. Use default model 2. */
  670. rp->model = 2;
  671. rp->rows = 24;
  672. rp->cols = 80;
  673. return 0;
  674. }
  675. return rc;
  676. }
  677. static int
  678. raw3270_reset_device(struct raw3270 *rp)
  679. {
  680. int rc;
  681. down(&raw3270_init_sem);
  682. memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
  683. memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
  684. /* Store reset data stream to raw3270_init_data/raw3270_init_request */
  685. raw3270_init_data[0] = TW_KR;
  686. INIT_LIST_HEAD(&raw3270_init_request.list);
  687. raw3270_init_request.ccw.cmd_code = TC_EWRITEA;
  688. raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
  689. raw3270_init_request.ccw.count = 1;
  690. raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
  691. rp->view = &raw3270_init_view;
  692. raw3270_init_view.dev = rp;
  693. rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
  694. raw3270_init_view.dev = 0;
  695. rp->view = 0;
  696. up(&raw3270_init_sem);
  697. return rc;
  698. }
  699. int
  700. raw3270_reset(struct raw3270_view *view)
  701. {
  702. struct raw3270 *rp;
  703. int rc;
  704. rp = view->dev;
  705. if (!rp || rp->view != view)
  706. rc = -EACCES;
  707. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  708. rc = -ENODEV;
  709. else
  710. rc = raw3270_reset_device(view->dev);
  711. return rc;
  712. }
  713. /*
  714. * Setup new 3270 device.
  715. */
  716. static int
  717. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  718. {
  719. struct list_head *l;
  720. struct raw3270 *tmp;
  721. int minor;
  722. memset(rp, 0, sizeof(struct raw3270));
  723. /* Copy ebcdic -> ascii translation table. */
  724. memcpy(ascebc, _ascebc, 256);
  725. if (tubxcorrect) {
  726. /* correct brackets and circumflex */
  727. ascebc['['] = 0xad;
  728. ascebc[']'] = 0xbd;
  729. ascebc['^'] = 0xb0;
  730. }
  731. rp->ascebc = ascebc;
  732. /* Set defaults. */
  733. rp->rows = 24;
  734. rp->cols = 80;
  735. INIT_LIST_HEAD(&rp->req_queue);
  736. INIT_LIST_HEAD(&rp->view_list);
  737. /*
  738. * Add device to list and find the smallest unused minor
  739. * number for it. Note: there is no device with minor 0,
  740. * see special case for fs3270.c:fs3270_open().
  741. */
  742. mutex_lock(&raw3270_mutex);
  743. /* Keep the list sorted. */
  744. minor = RAW3270_FIRSTMINOR;
  745. rp->minor = -1;
  746. list_for_each(l, &raw3270_devices) {
  747. tmp = list_entry(l, struct raw3270, list);
  748. if (tmp->minor > minor) {
  749. rp->minor = minor;
  750. __list_add(&rp->list, l->prev, l);
  751. break;
  752. }
  753. minor++;
  754. }
  755. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  756. rp->minor = minor;
  757. list_add_tail(&rp->list, &raw3270_devices);
  758. }
  759. mutex_unlock(&raw3270_mutex);
  760. /* No free minor number? Then give up. */
  761. if (rp->minor == -1)
  762. return -EUSERS;
  763. rp->cdev = cdev;
  764. cdev->dev.driver_data = rp;
  765. cdev->handler = raw3270_irq;
  766. return 0;
  767. }
  768. #ifdef CONFIG_TN3270_CONSOLE
  769. /*
  770. * Setup 3270 device configured as console.
  771. */
  772. struct raw3270 *
  773. raw3270_setup_console(struct ccw_device *cdev)
  774. {
  775. struct raw3270 *rp;
  776. char *ascebc;
  777. int rc;
  778. rp = (struct raw3270 *) alloc_bootmem(sizeof(struct raw3270));
  779. ascebc = (char *) alloc_bootmem(256);
  780. rc = raw3270_setup_device(cdev, rp, ascebc);
  781. if (rc)
  782. return ERR_PTR(rc);
  783. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  784. rc = raw3270_reset_device(rp);
  785. if (rc)
  786. return ERR_PTR(rc);
  787. rc = raw3270_size_device(rp);
  788. if (rc)
  789. return ERR_PTR(rc);
  790. rc = raw3270_reset_device(rp);
  791. if (rc)
  792. return ERR_PTR(rc);
  793. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  794. return rp;
  795. }
  796. void
  797. raw3270_wait_cons_dev(struct raw3270 *rp)
  798. {
  799. unsigned long flags;
  800. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  801. wait_cons_dev();
  802. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  803. }
  804. #endif
  805. /*
  806. * Create a 3270 device structure.
  807. */
  808. static struct raw3270 *
  809. raw3270_create_device(struct ccw_device *cdev)
  810. {
  811. struct raw3270 *rp;
  812. char *ascebc;
  813. int rc;
  814. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL);
  815. if (!rp)
  816. return ERR_PTR(-ENOMEM);
  817. ascebc = kmalloc(256, GFP_KERNEL);
  818. if (!ascebc) {
  819. kfree(rp);
  820. return ERR_PTR(-ENOMEM);
  821. }
  822. rc = raw3270_setup_device(cdev, rp, ascebc);
  823. if (rc) {
  824. kfree(rp->ascebc);
  825. kfree(rp);
  826. rp = ERR_PTR(rc);
  827. }
  828. /* Get reference to ccw_device structure. */
  829. get_device(&cdev->dev);
  830. return rp;
  831. }
  832. /*
  833. * Activate a view.
  834. */
  835. int
  836. raw3270_activate_view(struct raw3270_view *view)
  837. {
  838. struct raw3270 *rp;
  839. struct raw3270_view *oldview, *nv;
  840. unsigned long flags;
  841. int rc;
  842. rp = view->dev;
  843. if (!rp)
  844. return -ENODEV;
  845. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  846. if (rp->view == view)
  847. rc = 0;
  848. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  849. rc = -ENODEV;
  850. else {
  851. oldview = 0;
  852. if (rp->view) {
  853. oldview = rp->view;
  854. oldview->fn->deactivate(oldview);
  855. }
  856. rp->view = view;
  857. rc = view->fn->activate(view);
  858. if (rc) {
  859. /* Didn't work. Try to reactivate the old view. */
  860. rp->view = oldview;
  861. if (!oldview || oldview->fn->activate(oldview) != 0) {
  862. /* Didn't work as well. Try any other view. */
  863. list_for_each_entry(nv, &rp->view_list, list)
  864. if (nv != view && nv != oldview) {
  865. rp->view = nv;
  866. if (nv->fn->activate(nv) == 0)
  867. break;
  868. rp->view = 0;
  869. }
  870. }
  871. }
  872. }
  873. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  874. return rc;
  875. }
  876. /*
  877. * Deactivate current view.
  878. */
  879. void
  880. raw3270_deactivate_view(struct raw3270_view *view)
  881. {
  882. unsigned long flags;
  883. struct raw3270 *rp;
  884. rp = view->dev;
  885. if (!rp)
  886. return;
  887. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  888. if (rp->view == view) {
  889. view->fn->deactivate(view);
  890. rp->view = 0;
  891. /* Move deactivated view to end of list. */
  892. list_del_init(&view->list);
  893. list_add_tail(&view->list, &rp->view_list);
  894. /* Try to activate another view. */
  895. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  896. list_for_each_entry(view, &rp->view_list, list) {
  897. rp->view = view;
  898. if (view->fn->activate(view) == 0)
  899. break;
  900. rp->view = 0;
  901. }
  902. }
  903. }
  904. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  905. }
  906. /*
  907. * Add view to device with minor "minor".
  908. */
  909. int
  910. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  911. {
  912. unsigned long flags;
  913. struct raw3270 *rp;
  914. int rc;
  915. if (minor <= 0)
  916. return -ENODEV;
  917. mutex_lock(&raw3270_mutex);
  918. rc = -ENODEV;
  919. list_for_each_entry(rp, &raw3270_devices, list) {
  920. if (rp->minor != minor)
  921. continue;
  922. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  923. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  924. atomic_set(&view->ref_count, 2);
  925. view->dev = rp;
  926. view->fn = fn;
  927. view->model = rp->model;
  928. view->rows = rp->rows;
  929. view->cols = rp->cols;
  930. view->ascebc = rp->ascebc;
  931. spin_lock_init(&view->lock);
  932. list_add(&view->list, &rp->view_list);
  933. rc = 0;
  934. }
  935. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  936. break;
  937. }
  938. mutex_unlock(&raw3270_mutex);
  939. return rc;
  940. }
  941. /*
  942. * Find specific view of device with minor "minor".
  943. */
  944. struct raw3270_view *
  945. raw3270_find_view(struct raw3270_fn *fn, int minor)
  946. {
  947. struct raw3270 *rp;
  948. struct raw3270_view *view, *tmp;
  949. unsigned long flags;
  950. mutex_lock(&raw3270_mutex);
  951. view = ERR_PTR(-ENODEV);
  952. list_for_each_entry(rp, &raw3270_devices, list) {
  953. if (rp->minor != minor)
  954. continue;
  955. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  956. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  957. view = ERR_PTR(-ENOENT);
  958. list_for_each_entry(tmp, &rp->view_list, list) {
  959. if (tmp->fn == fn) {
  960. raw3270_get_view(tmp);
  961. view = tmp;
  962. break;
  963. }
  964. }
  965. }
  966. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  967. break;
  968. }
  969. mutex_unlock(&raw3270_mutex);
  970. return view;
  971. }
  972. /*
  973. * Remove view from device and free view structure via call to view->fn->free.
  974. */
  975. void
  976. raw3270_del_view(struct raw3270_view *view)
  977. {
  978. unsigned long flags;
  979. struct raw3270 *rp;
  980. struct raw3270_view *nv;
  981. rp = view->dev;
  982. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  983. if (rp->view == view) {
  984. view->fn->deactivate(view);
  985. rp->view = 0;
  986. }
  987. list_del_init(&view->list);
  988. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  989. /* Try to activate another view. */
  990. list_for_each_entry(nv, &rp->view_list, list) {
  991. if (nv->fn->activate(nv) == 0) {
  992. rp->view = nv;
  993. break;
  994. }
  995. }
  996. }
  997. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  998. /* Wait for reference counter to drop to zero. */
  999. atomic_dec(&view->ref_count);
  1000. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  1001. if (view->fn->free)
  1002. view->fn->free(view);
  1003. }
  1004. /*
  1005. * Remove a 3270 device structure.
  1006. */
  1007. static void
  1008. raw3270_delete_device(struct raw3270 *rp)
  1009. {
  1010. struct ccw_device *cdev;
  1011. /* Remove from device chain. */
  1012. mutex_lock(&raw3270_mutex);
  1013. if (rp->clttydev)
  1014. class_device_destroy(class3270,
  1015. MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1016. if (rp->cltubdev)
  1017. class_device_destroy(class3270,
  1018. MKDEV(IBM_FS3270_MAJOR, rp->minor));
  1019. list_del_init(&rp->list);
  1020. mutex_unlock(&raw3270_mutex);
  1021. /* Disconnect from ccw_device. */
  1022. cdev = rp->cdev;
  1023. rp->cdev = 0;
  1024. cdev->dev.driver_data = 0;
  1025. cdev->handler = 0;
  1026. /* Put ccw_device structure. */
  1027. put_device(&cdev->dev);
  1028. /* Now free raw3270 structure. */
  1029. kfree(rp->ascebc);
  1030. kfree(rp);
  1031. }
  1032. static int
  1033. raw3270_probe (struct ccw_device *cdev)
  1034. {
  1035. return 0;
  1036. }
  1037. /*
  1038. * Additional attributes for a 3270 device
  1039. */
  1040. static ssize_t
  1041. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1042. {
  1043. return snprintf(buf, PAGE_SIZE, "%i\n",
  1044. ((struct raw3270 *) dev->driver_data)->model);
  1045. }
  1046. static DEVICE_ATTR(model, 0444, raw3270_model_show, 0);
  1047. static ssize_t
  1048. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1049. {
  1050. return snprintf(buf, PAGE_SIZE, "%i\n",
  1051. ((struct raw3270 *) dev->driver_data)->rows);
  1052. }
  1053. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, 0);
  1054. static ssize_t
  1055. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1056. {
  1057. return snprintf(buf, PAGE_SIZE, "%i\n",
  1058. ((struct raw3270 *) dev->driver_data)->cols);
  1059. }
  1060. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, 0);
  1061. static struct attribute * raw3270_attrs[] = {
  1062. &dev_attr_model.attr,
  1063. &dev_attr_rows.attr,
  1064. &dev_attr_columns.attr,
  1065. NULL,
  1066. };
  1067. static struct attribute_group raw3270_attr_group = {
  1068. .attrs = raw3270_attrs,
  1069. };
  1070. static void
  1071. raw3270_create_attributes(struct raw3270 *rp)
  1072. {
  1073. //FIXME: check return code
  1074. sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1075. rp->clttydev =
  1076. class_device_create(class3270, NULL,
  1077. MKDEV(IBM_TTY3270_MAJOR, rp->minor),
  1078. &rp->cdev->dev, "tty%s",
  1079. rp->cdev->dev.bus_id);
  1080. rp->cltubdev =
  1081. class_device_create(class3270, NULL,
  1082. MKDEV(IBM_FS3270_MAJOR, rp->minor),
  1083. &rp->cdev->dev, "tub%s",
  1084. rp->cdev->dev.bus_id);
  1085. }
  1086. /*
  1087. * Notifier for device addition/removal
  1088. */
  1089. struct raw3270_notifier {
  1090. struct list_head list;
  1091. void (*notifier)(int, int);
  1092. };
  1093. static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
  1094. int raw3270_register_notifier(void (*notifier)(int, int))
  1095. {
  1096. struct raw3270_notifier *np;
  1097. struct raw3270 *rp;
  1098. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1099. if (!np)
  1100. return -ENOMEM;
  1101. np->notifier = notifier;
  1102. mutex_lock(&raw3270_mutex);
  1103. list_add_tail(&np->list, &raw3270_notifier);
  1104. list_for_each_entry(rp, &raw3270_devices, list) {
  1105. get_device(&rp->cdev->dev);
  1106. notifier(rp->minor, 1);
  1107. }
  1108. mutex_unlock(&raw3270_mutex);
  1109. return 0;
  1110. }
  1111. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1112. {
  1113. struct raw3270_notifier *np;
  1114. mutex_lock(&raw3270_mutex);
  1115. list_for_each_entry(np, &raw3270_notifier, list)
  1116. if (np->notifier == notifier) {
  1117. list_del(&np->list);
  1118. kfree(np);
  1119. break;
  1120. }
  1121. mutex_unlock(&raw3270_mutex);
  1122. }
  1123. /*
  1124. * Set 3270 device online.
  1125. */
  1126. static int
  1127. raw3270_set_online (struct ccw_device *cdev)
  1128. {
  1129. struct raw3270 *rp;
  1130. struct raw3270_notifier *np;
  1131. int rc;
  1132. rp = raw3270_create_device(cdev);
  1133. if (IS_ERR(rp))
  1134. return PTR_ERR(rp);
  1135. rc = raw3270_reset_device(rp);
  1136. if (rc)
  1137. goto failure;
  1138. rc = raw3270_size_device(rp);
  1139. if (rc)
  1140. goto failure;
  1141. rc = raw3270_reset_device(rp);
  1142. if (rc)
  1143. goto failure;
  1144. raw3270_create_attributes(rp);
  1145. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1146. mutex_lock(&raw3270_mutex);
  1147. list_for_each_entry(np, &raw3270_notifier, list)
  1148. np->notifier(rp->minor, 1);
  1149. mutex_unlock(&raw3270_mutex);
  1150. return 0;
  1151. failure:
  1152. raw3270_delete_device(rp);
  1153. return rc;
  1154. }
  1155. /*
  1156. * Remove 3270 device structure.
  1157. */
  1158. static void
  1159. raw3270_remove (struct ccw_device *cdev)
  1160. {
  1161. unsigned long flags;
  1162. struct raw3270 *rp;
  1163. struct raw3270_view *v;
  1164. struct raw3270_notifier *np;
  1165. rp = cdev->dev.driver_data;
  1166. /*
  1167. * _remove is the opposite of _probe; it's probe that
  1168. * should set up rp. raw3270_remove gets entered for
  1169. * devices even if they haven't been varied online.
  1170. * Thus, rp may validly be NULL here.
  1171. */
  1172. if (rp == NULL)
  1173. return;
  1174. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1175. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1176. /* Deactivate current view and remove all views. */
  1177. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1178. if (rp->view) {
  1179. rp->view->fn->deactivate(rp->view);
  1180. rp->view = 0;
  1181. }
  1182. while (!list_empty(&rp->view_list)) {
  1183. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1184. if (v->fn->release)
  1185. v->fn->release(v);
  1186. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1187. raw3270_del_view(v);
  1188. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1189. }
  1190. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1191. mutex_lock(&raw3270_mutex);
  1192. list_for_each_entry(np, &raw3270_notifier, list)
  1193. np->notifier(rp->minor, 0);
  1194. mutex_unlock(&raw3270_mutex);
  1195. /* Reset 3270 device. */
  1196. raw3270_reset_device(rp);
  1197. /* And finally remove it. */
  1198. raw3270_delete_device(rp);
  1199. }
  1200. /*
  1201. * Set 3270 device offline.
  1202. */
  1203. static int
  1204. raw3270_set_offline (struct ccw_device *cdev)
  1205. {
  1206. struct raw3270 *rp;
  1207. rp = cdev->dev.driver_data;
  1208. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1209. return -EBUSY;
  1210. raw3270_remove(cdev);
  1211. return 0;
  1212. }
  1213. static struct ccw_device_id raw3270_id[] = {
  1214. { CCW_DEVICE(0x3270, 0) },
  1215. { CCW_DEVICE(0x3271, 0) },
  1216. { CCW_DEVICE(0x3272, 0) },
  1217. { CCW_DEVICE(0x3273, 0) },
  1218. { CCW_DEVICE(0x3274, 0) },
  1219. { CCW_DEVICE(0x3275, 0) },
  1220. { CCW_DEVICE(0x3276, 0) },
  1221. { CCW_DEVICE(0x3277, 0) },
  1222. { CCW_DEVICE(0x3278, 0) },
  1223. { CCW_DEVICE(0x3279, 0) },
  1224. { CCW_DEVICE(0x3174, 0) },
  1225. { /* end of list */ },
  1226. };
  1227. static struct ccw_driver raw3270_ccw_driver = {
  1228. .name = "3270",
  1229. .owner = THIS_MODULE,
  1230. .ids = raw3270_id,
  1231. .probe = &raw3270_probe,
  1232. .remove = &raw3270_remove,
  1233. .set_online = &raw3270_set_online,
  1234. .set_offline = &raw3270_set_offline,
  1235. };
  1236. static int
  1237. raw3270_init(void)
  1238. {
  1239. struct raw3270 *rp;
  1240. int rc;
  1241. if (raw3270_registered)
  1242. return 0;
  1243. raw3270_registered = 1;
  1244. rc = ccw_driver_register(&raw3270_ccw_driver);
  1245. if (rc == 0) {
  1246. /* Create attributes for early (= console) device. */
  1247. mutex_lock(&raw3270_mutex);
  1248. class3270 = class_create(THIS_MODULE, "3270");
  1249. list_for_each_entry(rp, &raw3270_devices, list) {
  1250. get_device(&rp->cdev->dev);
  1251. raw3270_create_attributes(rp);
  1252. }
  1253. mutex_unlock(&raw3270_mutex);
  1254. }
  1255. return rc;
  1256. }
  1257. static void
  1258. raw3270_exit(void)
  1259. {
  1260. ccw_driver_unregister(&raw3270_ccw_driver);
  1261. class_destroy(class3270);
  1262. }
  1263. MODULE_LICENSE("GPL");
  1264. module_init(raw3270_init);
  1265. module_exit(raw3270_exit);
  1266. EXPORT_SYMBOL(raw3270_request_alloc);
  1267. EXPORT_SYMBOL(raw3270_request_free);
  1268. EXPORT_SYMBOL(raw3270_request_reset);
  1269. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1270. EXPORT_SYMBOL(raw3270_request_add_data);
  1271. EXPORT_SYMBOL(raw3270_request_set_data);
  1272. EXPORT_SYMBOL(raw3270_request_set_idal);
  1273. EXPORT_SYMBOL(raw3270_buffer_address);
  1274. EXPORT_SYMBOL(raw3270_add_view);
  1275. EXPORT_SYMBOL(raw3270_del_view);
  1276. EXPORT_SYMBOL(raw3270_find_view);
  1277. EXPORT_SYMBOL(raw3270_activate_view);
  1278. EXPORT_SYMBOL(raw3270_deactivate_view);
  1279. EXPORT_SYMBOL(raw3270_start);
  1280. EXPORT_SYMBOL(raw3270_start_locked);
  1281. EXPORT_SYMBOL(raw3270_start_irq);
  1282. EXPORT_SYMBOL(raw3270_reset);
  1283. EXPORT_SYMBOL(raw3270_register_notifier);
  1284. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1285. EXPORT_SYMBOL(raw3270_wait_queue);