vmur.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * Linux driver for System z and s390 unit record devices
  3. * (z/VM virtual punch, reader, printer)
  4. *
  5. * Copyright IBM Corp. 2001, 2007
  6. * Authors: Malcolm Beattie <beattiem@uk.ibm.com>
  7. * Michael Holzheu <holzheu@de.ibm.com>
  8. * Frank Munzert <munzert@de.ibm.com>
  9. */
  10. #include <linux/cdev.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/cio.h>
  13. #include <asm/ccwdev.h>
  14. #include <asm/debug.h>
  15. #include "vmur.h"
  16. /*
  17. * Driver overview
  18. *
  19. * Unit record device support is implemented as a character device driver.
  20. * We can fit at least 16 bits into a device minor number and use the
  21. * simple method of mapping a character device number with minor abcd
  22. * to the unit record device with devno abcd.
  23. * I/O to virtual unit record devices is handled as follows:
  24. * Reads: Diagnose code 0x14 (input spool file manipulation)
  25. * is used to read spool data page-wise.
  26. * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
  27. * is available by reading sysfs attr reclen. Each write() to the device
  28. * must specify an integral multiple (maximal 511) of reclen.
  29. */
  30. static char ur_banner[] = "z/VM virtual unit record device driver";
  31. MODULE_AUTHOR("IBM Corporation");
  32. MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
  33. MODULE_LICENSE("GPL");
  34. #define PRINTK_HEADER "vmur: "
  35. static dev_t ur_first_dev_maj_min;
  36. static struct class *vmur_class;
  37. static struct debug_info *vmur_dbf;
  38. /* We put the device's record length (for writes) in the driver_info field */
  39. static struct ccw_device_id ur_ids[] = {
  40. { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
  41. { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
  42. { /* end of list */ }
  43. };
  44. MODULE_DEVICE_TABLE(ccw, ur_ids);
  45. static int ur_probe(struct ccw_device *cdev);
  46. static void ur_remove(struct ccw_device *cdev);
  47. static int ur_set_online(struct ccw_device *cdev);
  48. static int ur_set_offline(struct ccw_device *cdev);
  49. static struct ccw_driver ur_driver = {
  50. .name = "vmur",
  51. .owner = THIS_MODULE,
  52. .ids = ur_ids,
  53. .probe = ur_probe,
  54. .remove = ur_remove,
  55. .set_online = ur_set_online,
  56. .set_offline = ur_set_offline,
  57. };
  58. /*
  59. * Allocation, freeing, getting and putting of urdev structures
  60. */
  61. static struct urdev *urdev_alloc(struct ccw_device *cdev)
  62. {
  63. struct urdev *urd;
  64. urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
  65. if (!urd)
  66. return NULL;
  67. urd->cdev = cdev;
  68. urd->reclen = cdev->id.driver_info;
  69. ccw_device_get_id(cdev, &urd->dev_id);
  70. mutex_init(&urd->io_mutex);
  71. mutex_init(&urd->open_mutex);
  72. return urd;
  73. }
  74. static void urdev_free(struct urdev *urd)
  75. {
  76. kfree(urd);
  77. }
  78. /*
  79. * This is how the character device driver gets a reference to a
  80. * ur device. When this call returns successfully, a reference has
  81. * been taken (by get_device) on the underlying kobject. The recipient
  82. * of this urdev pointer must eventually drop it with urdev_put(urd)
  83. * which does the corresponding put_device().
  84. */
  85. static struct urdev *urdev_get_from_devno(u16 devno)
  86. {
  87. char bus_id[16];
  88. struct ccw_device *cdev;
  89. sprintf(bus_id, "0.0.%04x", devno);
  90. cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
  91. if (!cdev)
  92. return NULL;
  93. return cdev->dev.driver_data;
  94. }
  95. static void urdev_put(struct urdev *urd)
  96. {
  97. put_device(&urd->cdev->dev);
  98. }
  99. /*
  100. * Low-level functions to do I/O to a ur device.
  101. * alloc_chan_prog
  102. * do_ur_io
  103. * ur_int_handler
  104. *
  105. * alloc_chan_prog allocates and builds the channel program
  106. *
  107. * do_ur_io issues the channel program to the device and blocks waiting
  108. * on a completion event it publishes at urd->io_done. The function
  109. * serialises itself on the device's mutex so that only one I/O
  110. * is issued at a time (and that I/O is synchronous).
  111. *
  112. * ur_int_handler catches the "I/O done" interrupt, writes the
  113. * subchannel status word into the scsw member of the urdev structure
  114. * and complete()s the io_done to wake the waiting do_ur_io.
  115. *
  116. * The caller of do_ur_io is responsible for kfree()ing the channel program
  117. * address pointer that alloc_chan_prog returned.
  118. */
  119. /*
  120. * alloc_chan_prog
  121. * The channel program we use is write commands chained together
  122. * with a final NOP CCW command-chained on (which ensures that CE and DE
  123. * are presented together in a single interrupt instead of as separate
  124. * interrupts unless an incorrect length indication kicks in first). The
  125. * data length in each CCW is reclen. The caller must ensure that count
  126. * is an integral multiple of reclen.
  127. * The channel program pointer returned by this function must be freed
  128. * with kfree. The caller is responsible for checking that
  129. * count/reclen is not ridiculously large.
  130. */
  131. static struct ccw1 *alloc_chan_prog(char *buf, size_t count, size_t reclen)
  132. {
  133. size_t num_ccws;
  134. struct ccw1 *cpa;
  135. int i;
  136. TRACE("alloc_chan_prog(%p, %zu, %zu)\n", buf, count, reclen);
  137. /*
  138. * We chain a NOP onto the writes to force CE+DE together.
  139. * That means we allocate room for CCWs to cover count/reclen
  140. * records plus a NOP.
  141. */
  142. num_ccws = count / reclen + 1;
  143. cpa = kmalloc(num_ccws * sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
  144. if (!cpa)
  145. return NULL;
  146. for (i = 0; count; i++) {
  147. cpa[i].cmd_code = WRITE_CCW_CMD;
  148. cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
  149. cpa[i].count = reclen;
  150. cpa[i].cda = __pa(buf);
  151. buf += reclen;
  152. count -= reclen;
  153. }
  154. /* The following NOP CCW forces CE+DE to be presented together */
  155. cpa[i].cmd_code = CCW_CMD_NOOP;
  156. cpa[i].flags = 0;
  157. cpa[i].count = 0;
  158. cpa[i].cda = 0;
  159. return cpa;
  160. }
  161. static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
  162. {
  163. int rc;
  164. struct ccw_device *cdev = urd->cdev;
  165. DECLARE_COMPLETION(event);
  166. TRACE("do_ur_io: cpa=%p\n", cpa);
  167. rc = mutex_lock_interruptible(&urd->io_mutex);
  168. if (rc)
  169. return rc;
  170. urd->io_done = &event;
  171. spin_lock_irq(get_ccwdev_lock(cdev));
  172. rc = ccw_device_start(cdev, cpa, 1, 0, 0);
  173. spin_unlock_irq(get_ccwdev_lock(cdev));
  174. TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
  175. if (rc)
  176. goto out;
  177. wait_for_completion(&event);
  178. TRACE("do_ur_io: I/O complete\n");
  179. rc = 0;
  180. out:
  181. mutex_unlock(&urd->io_mutex);
  182. return rc;
  183. }
  184. /*
  185. * ur interrupt handler, called from the ccw_device layer
  186. */
  187. static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
  188. struct irb *irb)
  189. {
  190. struct urdev *urd;
  191. TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
  192. intparm, irb->scsw.cstat, irb->scsw.dstat, irb->scsw.count);
  193. if (!intparm) {
  194. TRACE("ur_int_handler: unsolicited interrupt\n");
  195. return;
  196. }
  197. urd = cdev->dev.driver_data;
  198. /* On special conditions irb is an error pointer */
  199. if (IS_ERR(irb))
  200. urd->io_request_rc = PTR_ERR(irb);
  201. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  202. urd->io_request_rc = 0;
  203. else
  204. urd->io_request_rc = -EIO;
  205. complete(urd->io_done);
  206. }
  207. /*
  208. * reclen sysfs attribute - The record length to be used for write CCWs
  209. */
  210. static ssize_t ur_attr_reclen_show(struct device *dev,
  211. struct device_attribute *attr, char *buf)
  212. {
  213. struct urdev *urd = dev->driver_data;
  214. return sprintf(buf, "%zu\n", urd->reclen);
  215. }
  216. static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
  217. static int ur_create_attributes(struct device *dev)
  218. {
  219. return device_create_file(dev, &dev_attr_reclen);
  220. }
  221. static void ur_remove_attributes(struct device *dev)
  222. {
  223. device_remove_file(dev, &dev_attr_reclen);
  224. }
  225. /*
  226. * diagnose code 0x210 - retrieve device information
  227. * cc=0 normal completion, we have a real device
  228. * cc=1 CP paging error
  229. * cc=2 The virtual device exists, but is not associated with a real device
  230. * cc=3 Invalid device address, or the virtual device does not exist
  231. */
  232. static int get_urd_class(struct urdev *urd)
  233. {
  234. static struct diag210 ur_diag210;
  235. int cc;
  236. ur_diag210.vrdcdvno = urd->dev_id.devno;
  237. ur_diag210.vrdclen = sizeof(struct diag210);
  238. cc = diag210(&ur_diag210);
  239. switch (cc) {
  240. case 0:
  241. return -ENOTSUPP;
  242. case 2:
  243. return ur_diag210.vrdcvcla; /* virtual device class */
  244. case 3:
  245. return -ENODEV;
  246. default:
  247. return -EIO;
  248. }
  249. }
  250. /*
  251. * Allocation and freeing of urfile structures
  252. */
  253. static struct urfile *urfile_alloc(struct urdev *urd)
  254. {
  255. struct urfile *urf;
  256. urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
  257. if (!urf)
  258. return NULL;
  259. urf->urd = urd;
  260. TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
  261. urf->dev_reclen);
  262. return urf;
  263. }
  264. static void urfile_free(struct urfile *urf)
  265. {
  266. TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
  267. kfree(urf);
  268. }
  269. /*
  270. * The fops implementation of the character device driver
  271. */
  272. static ssize_t do_write(struct urdev *urd, const char __user *udata,
  273. size_t count, size_t reclen, loff_t *ppos)
  274. {
  275. struct ccw1 *cpa;
  276. char *buf;
  277. int rc;
  278. /* Data buffer must be under 2GB line for fmt1 CCWs: hence GFP_DMA */
  279. buf = kmalloc(count, GFP_KERNEL | GFP_DMA);
  280. if (!buf)
  281. return -ENOMEM;
  282. if (copy_from_user(buf, udata, count)) {
  283. rc = -EFAULT;
  284. goto fail_kfree_buf;
  285. }
  286. cpa = alloc_chan_prog(buf, count, reclen);
  287. if (!cpa) {
  288. rc = -ENOMEM;
  289. goto fail_kfree_buf;
  290. }
  291. rc = do_ur_io(urd, cpa);
  292. if (rc)
  293. goto fail_kfree_cpa;
  294. if (urd->io_request_rc) {
  295. rc = urd->io_request_rc;
  296. goto fail_kfree_cpa;
  297. }
  298. *ppos += count;
  299. rc = count;
  300. fail_kfree_cpa:
  301. kfree(cpa);
  302. fail_kfree_buf:
  303. kfree(buf);
  304. return rc;
  305. }
  306. static ssize_t ur_write(struct file *file, const char __user *udata,
  307. size_t count, loff_t *ppos)
  308. {
  309. struct urfile *urf = file->private_data;
  310. TRACE("ur_write: count=%zu\n", count);
  311. if (count == 0)
  312. return 0;
  313. if (count % urf->dev_reclen)
  314. return -EINVAL; /* count must be a multiple of reclen */
  315. if (count > urf->dev_reclen * MAX_RECS_PER_IO)
  316. count = urf->dev_reclen * MAX_RECS_PER_IO;
  317. return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
  318. }
  319. static int do_diag_14(unsigned long rx, unsigned long ry1,
  320. unsigned long subcode)
  321. {
  322. register unsigned long _ry1 asm("2") = ry1;
  323. register unsigned long _ry2 asm("3") = subcode;
  324. int rc = 0;
  325. asm volatile(
  326. #ifdef CONFIG_64BIT
  327. " sam31\n"
  328. " diag %2,2,0x14\n"
  329. " sam64\n"
  330. #else
  331. " diag %2,2,0x14\n"
  332. #endif
  333. " ipm %0\n"
  334. " srl %0,28\n"
  335. : "=d" (rc), "+d" (_ry2)
  336. : "d" (rx), "d" (_ry1)
  337. : "cc");
  338. TRACE("diag 14: subcode=0x%lx, cc=%i\n", subcode, rc);
  339. return rc;
  340. }
  341. /*
  342. * diagnose code 0x14 subcode 0x0028 - position spool file to designated
  343. * record
  344. * cc=0 normal completion
  345. * cc=2 no file active on the virtual reader or device not ready
  346. * cc=3 record specified is beyond EOF
  347. */
  348. static int diag_position_to_record(int devno, int record)
  349. {
  350. int cc;
  351. cc = do_diag_14(record, devno, 0x28);
  352. switch (cc) {
  353. case 0:
  354. return 0;
  355. case 2:
  356. return -ENOMEDIUM;
  357. case 3:
  358. return -ENODATA; /* position beyond end of file */
  359. default:
  360. return -EIO;
  361. }
  362. }
  363. /*
  364. * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
  365. * cc=0 normal completion
  366. * cc=1 EOF reached
  367. * cc=2 no file active on the virtual reader, and no file eligible
  368. * cc=3 file already active on the virtual reader or specified virtual
  369. * reader does not exist or is not a reader
  370. */
  371. static int diag_read_file(int devno, char *buf)
  372. {
  373. int cc;
  374. cc = do_diag_14((unsigned long) buf, devno, 0x00);
  375. switch (cc) {
  376. case 0:
  377. return 0;
  378. case 1:
  379. return -ENODATA;
  380. case 2:
  381. return -ENOMEDIUM;
  382. default:
  383. return -EIO;
  384. }
  385. }
  386. static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
  387. loff_t *offs)
  388. {
  389. size_t len, copied, res;
  390. char *buf;
  391. int rc;
  392. u16 reclen;
  393. struct urdev *urd;
  394. urd = ((struct urfile *) file->private_data)->urd;
  395. reclen = ((struct urfile *) file->private_data)->file_reclen;
  396. rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
  397. if (rc == -ENODATA)
  398. return 0;
  399. if (rc)
  400. return rc;
  401. len = min((size_t) PAGE_SIZE, count);
  402. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  403. if (!buf)
  404. return -ENOMEM;
  405. copied = 0;
  406. res = (size_t) (*offs % PAGE_SIZE);
  407. do {
  408. rc = diag_read_file(urd->dev_id.devno, buf);
  409. if (rc == -ENODATA) {
  410. break;
  411. }
  412. if (rc)
  413. goto fail;
  414. if (reclen)
  415. *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
  416. len = min(count - copied, PAGE_SIZE - res);
  417. if (copy_to_user(ubuf + copied, buf + res, len)) {
  418. rc = -EFAULT;
  419. goto fail;
  420. }
  421. res = 0;
  422. copied += len;
  423. } while (copied != count);
  424. *offs += copied;
  425. rc = copied;
  426. fail:
  427. kfree(buf);
  428. return rc;
  429. }
  430. static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
  431. loff_t *offs)
  432. {
  433. struct urdev *urd;
  434. int rc;
  435. TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
  436. if (count == 0)
  437. return 0;
  438. urd = ((struct urfile *) file->private_data)->urd;
  439. rc = mutex_lock_interruptible(&urd->io_mutex);
  440. if (rc)
  441. return rc;
  442. rc = diag14_read(file, ubuf, count, offs);
  443. mutex_unlock(&urd->io_mutex);
  444. return rc;
  445. }
  446. /*
  447. * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
  448. * cc=0 normal completion
  449. * cc=1 no files on reader queue or no subsequent file
  450. * cc=2 spid specified is invalid
  451. */
  452. static int diag_read_next_file_info(struct file_control_block *buf, int spid)
  453. {
  454. int cc;
  455. cc = do_diag_14((unsigned long) buf, spid, 0xfff);
  456. switch (cc) {
  457. case 0:
  458. return 0;
  459. default:
  460. return -ENODATA;
  461. }
  462. }
  463. static int verify_device(struct urdev *urd)
  464. {
  465. struct file_control_block fcb;
  466. char *buf;
  467. int rc;
  468. switch (urd->class) {
  469. case DEV_CLASS_UR_O:
  470. return 0; /* no check needed here */
  471. case DEV_CLASS_UR_I:
  472. /* check for empty reader device (beginning of chain) */
  473. rc = diag_read_next_file_info(&fcb, 0);
  474. if (rc)
  475. return rc;
  476. /* open file on virtual reader */
  477. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  478. if (!buf)
  479. return -ENOMEM;
  480. rc = diag_read_file(urd->dev_id.devno, buf);
  481. kfree(buf);
  482. if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
  483. return rc;
  484. return 0;
  485. default:
  486. return -ENOTSUPP;
  487. }
  488. }
  489. static int get_file_reclen(struct urdev *urd)
  490. {
  491. struct file_control_block fcb;
  492. int rc;
  493. switch (urd->class) {
  494. case DEV_CLASS_UR_O:
  495. return 0;
  496. case DEV_CLASS_UR_I:
  497. rc = diag_read_next_file_info(&fcb, 0);
  498. if (rc)
  499. return rc;
  500. break;
  501. default:
  502. return -ENOTSUPP;
  503. }
  504. if (fcb.file_stat & FLG_CP_DUMP)
  505. return 0;
  506. return fcb.rec_len;
  507. }
  508. static int ur_open(struct inode *inode, struct file *file)
  509. {
  510. u16 devno;
  511. struct urdev *urd;
  512. struct urfile *urf;
  513. unsigned short accmode;
  514. int rc;
  515. accmode = file->f_flags & O_ACCMODE;
  516. if (accmode == O_RDWR)
  517. return -EACCES;
  518. /*
  519. * We treat the minor number as the devno of the ur device
  520. * to find in the driver tree.
  521. */
  522. devno = MINOR(file->f_dentry->d_inode->i_rdev);
  523. urd = urdev_get_from_devno(devno);
  524. if (!urd)
  525. return -ENXIO;
  526. if (file->f_flags & O_NONBLOCK) {
  527. if (!mutex_trylock(&urd->open_mutex)) {
  528. rc = -EBUSY;
  529. goto fail_put;
  530. }
  531. } else {
  532. if (mutex_lock_interruptible(&urd->open_mutex)) {
  533. rc = -ERESTARTSYS;
  534. goto fail_put;
  535. }
  536. }
  537. TRACE("ur_open\n");
  538. if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
  539. ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
  540. TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
  541. rc = -EACCES;
  542. goto fail_unlock;
  543. }
  544. rc = verify_device(urd);
  545. if (rc)
  546. goto fail_unlock;
  547. urf = urfile_alloc(urd);
  548. if (!urf) {
  549. rc = -ENOMEM;
  550. goto fail_unlock;
  551. }
  552. urf->dev_reclen = urd->reclen;
  553. rc = get_file_reclen(urd);
  554. if (rc < 0)
  555. goto fail_urfile_free;
  556. urf->file_reclen = rc;
  557. file->private_data = urf;
  558. return 0;
  559. fail_urfile_free:
  560. urfile_free(urf);
  561. fail_unlock:
  562. mutex_unlock(&urd->open_mutex);
  563. fail_put:
  564. urdev_put(urd);
  565. return rc;
  566. }
  567. static int ur_release(struct inode *inode, struct file *file)
  568. {
  569. struct urfile *urf = file->private_data;
  570. TRACE("ur_release\n");
  571. mutex_unlock(&urf->urd->open_mutex);
  572. urdev_put(urf->urd);
  573. urfile_free(urf);
  574. return 0;
  575. }
  576. static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
  577. {
  578. loff_t newpos;
  579. if ((file->f_flags & O_ACCMODE) != O_RDONLY)
  580. return -ESPIPE; /* seek allowed only for reader */
  581. if (offset % PAGE_SIZE)
  582. return -ESPIPE; /* only multiples of 4K allowed */
  583. switch (whence) {
  584. case 0: /* SEEK_SET */
  585. newpos = offset;
  586. break;
  587. case 1: /* SEEK_CUR */
  588. newpos = file->f_pos + offset;
  589. break;
  590. default:
  591. return -EINVAL;
  592. }
  593. file->f_pos = newpos;
  594. return newpos;
  595. }
  596. static struct file_operations ur_fops = {
  597. .owner = THIS_MODULE,
  598. .open = ur_open,
  599. .release = ur_release,
  600. .read = ur_read,
  601. .write = ur_write,
  602. .llseek = ur_llseek,
  603. };
  604. /*
  605. * ccw_device infrastructure:
  606. * ur_probe gets its own ref to the device (i.e. get_device),
  607. * creates the struct urdev, the device attributes, sets up
  608. * the interrupt handler and validates the virtual unit record device.
  609. * ur_remove removes the device attributes, frees the struct urdev
  610. * and drops (put_device) the ref to the device we got in ur_probe.
  611. */
  612. static int ur_probe(struct ccw_device *cdev)
  613. {
  614. struct urdev *urd;
  615. int rc;
  616. TRACE("ur_probe: cdev=%p state=%d\n", cdev, *(int *) cdev->private);
  617. if (!get_device(&cdev->dev))
  618. return -ENODEV;
  619. urd = urdev_alloc(cdev);
  620. if (!urd) {
  621. rc = -ENOMEM;
  622. goto fail;
  623. }
  624. rc = ur_create_attributes(&cdev->dev);
  625. if (rc) {
  626. rc = -ENOMEM;
  627. goto fail;
  628. }
  629. cdev->dev.driver_data = urd;
  630. cdev->handler = ur_int_handler;
  631. /* validate virtual unit record device */
  632. urd->class = get_urd_class(urd);
  633. if (urd->class < 0) {
  634. rc = urd->class;
  635. goto fail;
  636. }
  637. if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
  638. rc = -ENOTSUPP;
  639. goto fail;
  640. }
  641. return 0;
  642. fail:
  643. urdev_free(urd);
  644. put_device(&cdev->dev);
  645. return rc;
  646. }
  647. static void ur_remove(struct ccw_device *cdev)
  648. {
  649. struct urdev *urd = cdev->dev.driver_data;
  650. TRACE("ur_remove\n");
  651. if (cdev->online)
  652. ur_set_offline(cdev);
  653. ur_remove_attributes(&cdev->dev);
  654. urdev_free(urd);
  655. put_device(&cdev->dev);
  656. }
  657. static int ur_set_online(struct ccw_device *cdev)
  658. {
  659. struct urdev *urd;
  660. int minor, major, rc;
  661. char node_id[16];
  662. TRACE("ur_set_online: cdev=%p state=%d\n", cdev,
  663. *(int *) cdev->private);
  664. if (!try_module_get(ur_driver.owner))
  665. return -EINVAL;
  666. urd = (struct urdev *) cdev->dev.driver_data;
  667. minor = urd->dev_id.devno;
  668. major = MAJOR(ur_first_dev_maj_min);
  669. urd->char_device = cdev_alloc();
  670. if (!urd->char_device) {
  671. rc = -ENOMEM;
  672. goto fail_module_put;
  673. }
  674. cdev_init(urd->char_device, &ur_fops);
  675. urd->char_device->dev = MKDEV(major, minor);
  676. urd->char_device->owner = ur_fops.owner;
  677. rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
  678. if (rc)
  679. goto fail_free_cdev;
  680. if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
  681. if (urd->class == DEV_CLASS_UR_I)
  682. sprintf(node_id, "vmrdr-%s", cdev->dev.bus_id);
  683. if (urd->class == DEV_CLASS_UR_O)
  684. sprintf(node_id, "vmpun-%s", cdev->dev.bus_id);
  685. } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
  686. sprintf(node_id, "vmprt-%s", cdev->dev.bus_id);
  687. } else {
  688. rc = -ENOTSUPP;
  689. goto fail_free_cdev;
  690. }
  691. urd->device = device_create(vmur_class, NULL, urd->char_device->dev,
  692. "%s", node_id);
  693. if (IS_ERR(urd->device)) {
  694. rc = PTR_ERR(urd->device);
  695. TRACE("ur_set_online: device_create rc=%d\n", rc);
  696. goto fail_free_cdev;
  697. }
  698. return 0;
  699. fail_free_cdev:
  700. cdev_del(urd->char_device);
  701. fail_module_put:
  702. module_put(ur_driver.owner);
  703. return rc;
  704. }
  705. static int ur_set_offline(struct ccw_device *cdev)
  706. {
  707. struct urdev *urd;
  708. TRACE("ur_set_offline: cdev=%p cdev->private=%p state=%d\n",
  709. cdev, cdev->private, *(int *) cdev->private);
  710. urd = (struct urdev *) cdev->dev.driver_data;
  711. device_destroy(vmur_class, urd->char_device->dev);
  712. cdev_del(urd->char_device);
  713. module_put(ur_driver.owner);
  714. return 0;
  715. }
  716. /*
  717. * Module initialisation and cleanup
  718. */
  719. static int __init ur_init(void)
  720. {
  721. int rc;
  722. dev_t dev;
  723. if (!MACHINE_IS_VM) {
  724. PRINT_ERR("%s is only available under z/VM.\n", ur_banner);
  725. return -ENODEV;
  726. }
  727. vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
  728. if (!vmur_dbf)
  729. return -ENOMEM;
  730. rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
  731. if (rc)
  732. goto fail_free_dbf;
  733. debug_set_level(vmur_dbf, 6);
  734. rc = ccw_driver_register(&ur_driver);
  735. if (rc)
  736. goto fail_free_dbf;
  737. rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
  738. if (rc) {
  739. PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc);
  740. goto fail_unregister_driver;
  741. }
  742. ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
  743. vmur_class = class_create(THIS_MODULE, "vmur");
  744. if (IS_ERR(vmur_class)) {
  745. rc = PTR_ERR(vmur_class);
  746. goto fail_unregister_region;
  747. }
  748. PRINT_INFO("%s loaded.\n", ur_banner);
  749. return 0;
  750. fail_unregister_region:
  751. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  752. fail_unregister_driver:
  753. ccw_driver_unregister(&ur_driver);
  754. fail_free_dbf:
  755. debug_unregister(vmur_dbf);
  756. return rc;
  757. }
  758. static void __exit ur_exit(void)
  759. {
  760. class_destroy(vmur_class);
  761. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  762. ccw_driver_unregister(&ur_driver);
  763. debug_unregister(vmur_dbf);
  764. PRINT_INFO("%s unloaded.\n", ur_banner);
  765. }
  766. module_init(ur_init);
  767. module_exit(ur_exit);