raw3270.c 33 KB

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