pt.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. pt.c (c) 1998 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. This is the high-level driver for parallel port ATAPI tape
  5. drives based on chips supported by the paride module.
  6. The driver implements both rewinding and non-rewinding
  7. devices, filemarks, and the rewind ioctl. It allocates
  8. a small internal "bounce buffer" for each open device, but
  9. otherwise expects buffering and blocking to be done at the
  10. user level. As with most block-structured tapes, short
  11. writes are padded to full tape blocks, so reading back a file
  12. may return more data than was actually written.
  13. By default, the driver will autoprobe for a single parallel
  14. port ATAPI tape drive, but if their individual parameters are
  15. specified, the driver can handle up to 4 drives.
  16. The rewinding devices are named /dev/pt0, /dev/pt1, ...
  17. while the non-rewinding devices are /dev/npt0, /dev/npt1, etc.
  18. The behaviour of the pt driver can be altered by setting
  19. some parameters from the insmod command line. The following
  20. parameters are adjustable:
  21. drive0 These four arguments can be arrays of
  22. drive1 1-6 integers as follows:
  23. drive2
  24. drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
  25. Where,
  26. <prt> is the base of the parallel port address for
  27. the corresponding drive. (required)
  28. <pro> is the protocol number for the adapter that
  29. supports this drive. These numbers are
  30. logged by 'paride' when the protocol modules
  31. are initialised. (0 if not given)
  32. <uni> for those adapters that support chained
  33. devices, this is the unit selector for the
  34. chain of devices on the given port. It should
  35. be zero for devices that don't support chaining.
  36. (0 if not given)
  37. <mod> this can be -1 to choose the best mode, or one
  38. of the mode numbers supported by the adapter.
  39. (-1 if not given)
  40. <slv> ATAPI devices can be jumpered to master or slave.
  41. Set this to 0 to choose the master drive, 1 to
  42. choose the slave, -1 (the default) to choose the
  43. first drive found.
  44. <dly> some parallel ports require the driver to
  45. go more slowly. -1 sets a default value that
  46. should work with the chosen protocol. Otherwise,
  47. set this to a small integer, the larger it is
  48. the slower the port i/o. In some cases, setting
  49. this to zero will speed up the device. (default -1)
  50. major You may use this parameter to overide the
  51. default major number (96) that this driver
  52. will use. Be sure to change the device
  53. name as well.
  54. name This parameter is a character string that
  55. contains the name the kernel will use for this
  56. device (in /proc output, for instance).
  57. (default "pt").
  58. verbose This parameter controls the amount of logging
  59. that the driver will do. Set it to 0 for
  60. normal operation, 1 to see autoprobe progress
  61. messages, or 2 to see additional debugging
  62. output. (default 0)
  63. If this driver is built into the kernel, you can use
  64. the following command line parameters, with the same values
  65. as the corresponding module parameters listed above:
  66. pt.drive0
  67. pt.drive1
  68. pt.drive2
  69. pt.drive3
  70. In addition, you can use the parameter pt.disable to disable
  71. the driver entirely.
  72. */
  73. /* Changes:
  74. 1.01 GRG 1998.05.06 Round up transfer size, fix ready_wait,
  75. loosed interpretation of ATAPI standard
  76. for clearing error status.
  77. Eliminate sti();
  78. 1.02 GRG 1998.06.16 Eliminate an Ugh.
  79. 1.03 GRG 1998.08.15 Adjusted PT_TMO, use HZ in loop timing,
  80. extra debugging
  81. 1.04 GRG 1998.09.24 Repair minor coding error, added jumbo support
  82. */
  83. #define PT_VERSION "1.04"
  84. #define PT_MAJOR 96
  85. #define PT_NAME "pt"
  86. #define PT_UNITS 4
  87. #include <linux/types.h>
  88. /* Here are things one can override from the insmod command.
  89. Most are autoprobed by paride unless set here. Verbose is on
  90. by default.
  91. */
  92. static int verbose = 0;
  93. static int major = PT_MAJOR;
  94. static char *name = PT_NAME;
  95. static int disable = 0;
  96. static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
  97. static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
  98. static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
  99. static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
  100. static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
  101. #define D_PRT 0
  102. #define D_PRO 1
  103. #define D_UNI 2
  104. #define D_MOD 3
  105. #define D_SLV 4
  106. #define D_DLY 5
  107. #define DU (*drives[unit])
  108. /* end of parameters */
  109. #include <linux/module.h>
  110. #include <linux/init.h>
  111. #include <linux/fs.h>
  112. #include <linux/delay.h>
  113. #include <linux/slab.h>
  114. #include <linux/mtio.h>
  115. #include <linux/device.h>
  116. #include <linux/sched.h> /* current, TASK_*, schedule_timeout() */
  117. #include <linux/mutex.h>
  118. #include <asm/uaccess.h>
  119. module_param(verbose, int, 0);
  120. module_param(major, int, 0);
  121. module_param(name, charp, 0);
  122. module_param_array(drive0, int, NULL, 0);
  123. module_param_array(drive1, int, NULL, 0);
  124. module_param_array(drive2, int, NULL, 0);
  125. module_param_array(drive3, int, NULL, 0);
  126. #include "paride.h"
  127. #define PT_MAX_RETRIES 5
  128. #define PT_TMO 3000 /* interrupt timeout in jiffies */
  129. #define PT_SPIN_DEL 50 /* spin delay in micro-seconds */
  130. #define PT_RESET_TMO 30 /* 30 seconds */
  131. #define PT_READY_TMO 60 /* 60 seconds */
  132. #define PT_REWIND_TMO 1200 /* 20 minutes */
  133. #define PT_SPIN ((1000000/(HZ*PT_SPIN_DEL))*PT_TMO)
  134. #define STAT_ERR 0x00001
  135. #define STAT_INDEX 0x00002
  136. #define STAT_ECC 0x00004
  137. #define STAT_DRQ 0x00008
  138. #define STAT_SEEK 0x00010
  139. #define STAT_WRERR 0x00020
  140. #define STAT_READY 0x00040
  141. #define STAT_BUSY 0x00080
  142. #define STAT_SENSE 0x1f000
  143. #define ATAPI_TEST_READY 0x00
  144. #define ATAPI_REWIND 0x01
  145. #define ATAPI_REQ_SENSE 0x03
  146. #define ATAPI_READ_6 0x08
  147. #define ATAPI_WRITE_6 0x0a
  148. #define ATAPI_WFM 0x10
  149. #define ATAPI_IDENTIFY 0x12
  150. #define ATAPI_MODE_SENSE 0x1a
  151. #define ATAPI_LOG_SENSE 0x4d
  152. static DEFINE_MUTEX(pt_mutex);
  153. static int pt_open(struct inode *inode, struct file *file);
  154. static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  155. static int pt_release(struct inode *inode, struct file *file);
  156. static ssize_t pt_read(struct file *filp, char __user *buf,
  157. size_t count, loff_t * ppos);
  158. static ssize_t pt_write(struct file *filp, const char __user *buf,
  159. size_t count, loff_t * ppos);
  160. static int pt_detect(void);
  161. /* bits in tape->flags */
  162. #define PT_MEDIA 1
  163. #define PT_WRITE_OK 2
  164. #define PT_REWIND 4
  165. #define PT_WRITING 8
  166. #define PT_READING 16
  167. #define PT_EOF 32
  168. #define PT_NAMELEN 8
  169. #define PT_BUFSIZE 16384
  170. struct pt_unit {
  171. struct pi_adapter pia; /* interface to paride layer */
  172. struct pi_adapter *pi;
  173. int flags; /* various state flags */
  174. int last_sense; /* result of last request sense */
  175. int drive; /* drive */
  176. atomic_t available; /* 1 if access is available 0 otherwise */
  177. int bs; /* block size */
  178. int capacity; /* Size of tape in KB */
  179. int present; /* device present ? */
  180. char *bufptr;
  181. char name[PT_NAMELEN]; /* pf0, pf1, ... */
  182. };
  183. static int pt_identify(struct pt_unit *tape);
  184. static struct pt_unit pt[PT_UNITS];
  185. static char pt_scratch[512]; /* scratch block buffer */
  186. static void *par_drv; /* reference of parport driver */
  187. /* kernel glue structures */
  188. static const struct file_operations pt_fops = {
  189. .owner = THIS_MODULE,
  190. .read = pt_read,
  191. .write = pt_write,
  192. .unlocked_ioctl = pt_ioctl,
  193. .open = pt_open,
  194. .release = pt_release,
  195. .llseek = noop_llseek,
  196. };
  197. /* sysfs class support */
  198. static struct class *pt_class;
  199. static inline int status_reg(struct pi_adapter *pi)
  200. {
  201. return pi_read_regr(pi, 1, 6);
  202. }
  203. static inline int read_reg(struct pi_adapter *pi, int reg)
  204. {
  205. return pi_read_regr(pi, 0, reg);
  206. }
  207. static inline void write_reg(struct pi_adapter *pi, int reg, int val)
  208. {
  209. pi_write_regr(pi, 0, reg, val);
  210. }
  211. static inline u8 DRIVE(struct pt_unit *tape)
  212. {
  213. return 0xa0+0x10*tape->drive;
  214. }
  215. static int pt_wait(struct pt_unit *tape, int go, int stop, char *fun, char *msg)
  216. {
  217. int j, r, e, s, p;
  218. struct pi_adapter *pi = tape->pi;
  219. j = 0;
  220. while ((((r = status_reg(pi)) & go) || (stop && (!(r & stop))))
  221. && (j++ < PT_SPIN))
  222. udelay(PT_SPIN_DEL);
  223. if ((r & (STAT_ERR & stop)) || (j > PT_SPIN)) {
  224. s = read_reg(pi, 7);
  225. e = read_reg(pi, 1);
  226. p = read_reg(pi, 2);
  227. if (j > PT_SPIN)
  228. e |= 0x100;
  229. if (fun)
  230. printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
  231. " loop=%d phase=%d\n",
  232. tape->name, fun, msg, r, s, e, j, p);
  233. return (e << 8) + s;
  234. }
  235. return 0;
  236. }
  237. static int pt_command(struct pt_unit *tape, char *cmd, int dlen, char *fun)
  238. {
  239. struct pi_adapter *pi = tape->pi;
  240. pi_connect(pi);
  241. write_reg(pi, 6, DRIVE(tape));
  242. if (pt_wait(tape, STAT_BUSY | STAT_DRQ, 0, fun, "before command")) {
  243. pi_disconnect(pi);
  244. return -1;
  245. }
  246. write_reg(pi, 4, dlen % 256);
  247. write_reg(pi, 5, dlen / 256);
  248. write_reg(pi, 7, 0xa0); /* ATAPI packet command */
  249. if (pt_wait(tape, STAT_BUSY, STAT_DRQ, fun, "command DRQ")) {
  250. pi_disconnect(pi);
  251. return -1;
  252. }
  253. if (read_reg(pi, 2) != 1) {
  254. printk("%s: %s: command phase error\n", tape->name, fun);
  255. pi_disconnect(pi);
  256. return -1;
  257. }
  258. pi_write_block(pi, cmd, 12);
  259. return 0;
  260. }
  261. static int pt_completion(struct pt_unit *tape, char *buf, char *fun)
  262. {
  263. struct pi_adapter *pi = tape->pi;
  264. int r, s, n, p;
  265. r = pt_wait(tape, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR,
  266. fun, "completion");
  267. if (read_reg(pi, 7) & STAT_DRQ) {
  268. n = (((read_reg(pi, 4) + 256 * read_reg(pi, 5)) +
  269. 3) & 0xfffc);
  270. p = read_reg(pi, 2) & 3;
  271. if (p == 0)
  272. pi_write_block(pi, buf, n);
  273. if (p == 2)
  274. pi_read_block(pi, buf, n);
  275. }
  276. s = pt_wait(tape, STAT_BUSY, STAT_READY | STAT_ERR, fun, "data done");
  277. pi_disconnect(pi);
  278. return (r ? r : s);
  279. }
  280. static void pt_req_sense(struct pt_unit *tape, int quiet)
  281. {
  282. char rs_cmd[12] = { ATAPI_REQ_SENSE, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
  283. char buf[16];
  284. int r;
  285. r = pt_command(tape, rs_cmd, 16, "Request sense");
  286. mdelay(1);
  287. if (!r)
  288. pt_completion(tape, buf, "Request sense");
  289. tape->last_sense = -1;
  290. if (!r) {
  291. if (!quiet)
  292. printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
  293. tape->name, buf[2] & 0xf, buf[12], buf[13]);
  294. tape->last_sense = (buf[2] & 0xf) | ((buf[12] & 0xff) << 8)
  295. | ((buf[13] & 0xff) << 16);
  296. }
  297. }
  298. static int pt_atapi(struct pt_unit *tape, char *cmd, int dlen, char *buf, char *fun)
  299. {
  300. int r;
  301. r = pt_command(tape, cmd, dlen, fun);
  302. mdelay(1);
  303. if (!r)
  304. r = pt_completion(tape, buf, fun);
  305. if (r)
  306. pt_req_sense(tape, !fun);
  307. return r;
  308. }
  309. static void pt_sleep(int cs)
  310. {
  311. schedule_timeout_interruptible(cs);
  312. }
  313. static int pt_poll_dsc(struct pt_unit *tape, int pause, int tmo, char *msg)
  314. {
  315. struct pi_adapter *pi = tape->pi;
  316. int k, e, s;
  317. k = 0;
  318. e = 0;
  319. s = 0;
  320. while (k < tmo) {
  321. pt_sleep(pause);
  322. k++;
  323. pi_connect(pi);
  324. write_reg(pi, 6, DRIVE(tape));
  325. s = read_reg(pi, 7);
  326. e = read_reg(pi, 1);
  327. pi_disconnect(pi);
  328. if (s & (STAT_ERR | STAT_SEEK))
  329. break;
  330. }
  331. if ((k >= tmo) || (s & STAT_ERR)) {
  332. if (k >= tmo)
  333. printk("%s: %s DSC timeout\n", tape->name, msg);
  334. else
  335. printk("%s: %s stat=0x%x err=0x%x\n", tape->name, msg, s,
  336. e);
  337. pt_req_sense(tape, 0);
  338. return 0;
  339. }
  340. return 1;
  341. }
  342. static void pt_media_access_cmd(struct pt_unit *tape, int tmo, char *cmd, char *fun)
  343. {
  344. if (pt_command(tape, cmd, 0, fun)) {
  345. pt_req_sense(tape, 0);
  346. return;
  347. }
  348. pi_disconnect(tape->pi);
  349. pt_poll_dsc(tape, HZ, tmo, fun);
  350. }
  351. static void pt_rewind(struct pt_unit *tape)
  352. {
  353. char rw_cmd[12] = { ATAPI_REWIND, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  354. pt_media_access_cmd(tape, PT_REWIND_TMO, rw_cmd, "rewind");
  355. }
  356. static void pt_write_fm(struct pt_unit *tape)
  357. {
  358. char wm_cmd[12] = { ATAPI_WFM, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
  359. pt_media_access_cmd(tape, PT_TMO, wm_cmd, "write filemark");
  360. }
  361. #define DBMSG(msg) ((verbose>1)?(msg):NULL)
  362. static int pt_reset(struct pt_unit *tape)
  363. {
  364. struct pi_adapter *pi = tape->pi;
  365. int i, k, flg;
  366. int expect[5] = { 1, 1, 1, 0x14, 0xeb };
  367. pi_connect(pi);
  368. write_reg(pi, 6, DRIVE(tape));
  369. write_reg(pi, 7, 8);
  370. pt_sleep(20 * HZ / 1000);
  371. k = 0;
  372. while ((k++ < PT_RESET_TMO) && (status_reg(pi) & STAT_BUSY))
  373. pt_sleep(HZ / 10);
  374. flg = 1;
  375. for (i = 0; i < 5; i++)
  376. flg &= (read_reg(pi, i + 1) == expect[i]);
  377. if (verbose) {
  378. printk("%s: Reset (%d) signature = ", tape->name, k);
  379. for (i = 0; i < 5; i++)
  380. printk("%3x", read_reg(pi, i + 1));
  381. if (!flg)
  382. printk(" (incorrect)");
  383. printk("\n");
  384. }
  385. pi_disconnect(pi);
  386. return flg - 1;
  387. }
  388. static int pt_ready_wait(struct pt_unit *tape, int tmo)
  389. {
  390. char tr_cmd[12] = { ATAPI_TEST_READY, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  391. int k, p;
  392. k = 0;
  393. while (k < tmo) {
  394. tape->last_sense = 0;
  395. pt_atapi(tape, tr_cmd, 0, NULL, DBMSG("test unit ready"));
  396. p = tape->last_sense;
  397. if (!p)
  398. return 0;
  399. if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
  400. return p;
  401. k++;
  402. pt_sleep(HZ);
  403. }
  404. return 0x000020; /* timeout */
  405. }
  406. static void xs(char *buf, char *targ, int offs, int len)
  407. {
  408. int j, k, l;
  409. j = 0;
  410. l = 0;
  411. for (k = 0; k < len; k++)
  412. if ((buf[k + offs] != 0x20) || (buf[k + offs] != l))
  413. l = targ[j++] = buf[k + offs];
  414. if (l == 0x20)
  415. j--;
  416. targ[j] = 0;
  417. }
  418. static int xn(char *buf, int offs, int size)
  419. {
  420. int v, k;
  421. v = 0;
  422. for (k = 0; k < size; k++)
  423. v = v * 256 + (buf[k + offs] & 0xff);
  424. return v;
  425. }
  426. static int pt_identify(struct pt_unit *tape)
  427. {
  428. int dt, s;
  429. char *ms[2] = { "master", "slave" };
  430. char mf[10], id[18];
  431. char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
  432. char ms_cmd[12] =
  433. { ATAPI_MODE_SENSE, 0, 0x2a, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
  434. char ls_cmd[12] =
  435. { ATAPI_LOG_SENSE, 0, 0x71, 0, 0, 0, 0, 0, 36, 0, 0, 0 };
  436. char buf[36];
  437. s = pt_atapi(tape, id_cmd, 36, buf, "identify");
  438. if (s)
  439. return -1;
  440. dt = buf[0] & 0x1f;
  441. if (dt != 1) {
  442. if (verbose)
  443. printk("%s: Drive %d, unsupported type %d\n",
  444. tape->name, tape->drive, dt);
  445. return -1;
  446. }
  447. xs(buf, mf, 8, 8);
  448. xs(buf, id, 16, 16);
  449. tape->flags = 0;
  450. tape->capacity = 0;
  451. tape->bs = 0;
  452. if (!pt_ready_wait(tape, PT_READY_TMO))
  453. tape->flags |= PT_MEDIA;
  454. if (!pt_atapi(tape, ms_cmd, 36, buf, "mode sense")) {
  455. if (!(buf[2] & 0x80))
  456. tape->flags |= PT_WRITE_OK;
  457. tape->bs = xn(buf, 10, 2);
  458. }
  459. if (!pt_atapi(tape, ls_cmd, 36, buf, "log sense"))
  460. tape->capacity = xn(buf, 24, 4);
  461. printk("%s: %s %s, %s", tape->name, mf, id, ms[tape->drive]);
  462. if (!(tape->flags & PT_MEDIA))
  463. printk(", no media\n");
  464. else {
  465. if (!(tape->flags & PT_WRITE_OK))
  466. printk(", RO");
  467. printk(", blocksize %d, %d MB\n", tape->bs, tape->capacity / 1024);
  468. }
  469. return 0;
  470. }
  471. /*
  472. * returns 0, with id set if drive is detected
  473. * -1, if drive detection failed
  474. */
  475. static int pt_probe(struct pt_unit *tape)
  476. {
  477. if (tape->drive == -1) {
  478. for (tape->drive = 0; tape->drive <= 1; tape->drive++)
  479. if (!pt_reset(tape))
  480. return pt_identify(tape);
  481. } else {
  482. if (!pt_reset(tape))
  483. return pt_identify(tape);
  484. }
  485. return -1;
  486. }
  487. static int pt_detect(void)
  488. {
  489. struct pt_unit *tape;
  490. int specified = 0, found = 0;
  491. int unit;
  492. printk("%s: %s version %s, major %d\n", name, name, PT_VERSION, major);
  493. par_drv = pi_register_driver(name);
  494. if (!par_drv) {
  495. pr_err("failed to register %s driver\n", name);
  496. return -1;
  497. }
  498. specified = 0;
  499. for (unit = 0; unit < PT_UNITS; unit++) {
  500. struct pt_unit *tape = &pt[unit];
  501. tape->pi = &tape->pia;
  502. atomic_set(&tape->available, 1);
  503. tape->flags = 0;
  504. tape->last_sense = 0;
  505. tape->present = 0;
  506. tape->bufptr = NULL;
  507. tape->drive = DU[D_SLV];
  508. snprintf(tape->name, PT_NAMELEN, "%s%d", name, unit);
  509. if (!DU[D_PRT])
  510. continue;
  511. specified++;
  512. if (pi_init(tape->pi, 0, DU[D_PRT], DU[D_MOD], DU[D_UNI],
  513. DU[D_PRO], DU[D_DLY], pt_scratch, PI_PT,
  514. verbose, tape->name)) {
  515. if (!pt_probe(tape)) {
  516. tape->present = 1;
  517. found++;
  518. } else
  519. pi_release(tape->pi);
  520. }
  521. }
  522. if (specified == 0) {
  523. tape = pt;
  524. if (pi_init(tape->pi, 1, -1, -1, -1, -1, -1, pt_scratch,
  525. PI_PT, verbose, tape->name)) {
  526. if (!pt_probe(tape)) {
  527. tape->present = 1;
  528. found++;
  529. } else
  530. pi_release(tape->pi);
  531. }
  532. }
  533. if (found)
  534. return 0;
  535. pi_unregister_driver(par_drv);
  536. printk("%s: No ATAPI tape drive detected\n", name);
  537. return -1;
  538. }
  539. static int pt_open(struct inode *inode, struct file *file)
  540. {
  541. int unit = iminor(inode) & 0x7F;
  542. struct pt_unit *tape = pt + unit;
  543. int err;
  544. mutex_lock(&pt_mutex);
  545. if (unit >= PT_UNITS || (!tape->present)) {
  546. mutex_unlock(&pt_mutex);
  547. return -ENODEV;
  548. }
  549. err = -EBUSY;
  550. if (!atomic_dec_and_test(&tape->available))
  551. goto out;
  552. pt_identify(tape);
  553. err = -ENODEV;
  554. if (!(tape->flags & PT_MEDIA))
  555. goto out;
  556. err = -EROFS;
  557. if ((!(tape->flags & PT_WRITE_OK)) && (file->f_mode & FMODE_WRITE))
  558. goto out;
  559. if (!(iminor(inode) & 128))
  560. tape->flags |= PT_REWIND;
  561. err = -ENOMEM;
  562. tape->bufptr = kmalloc(PT_BUFSIZE, GFP_KERNEL);
  563. if (tape->bufptr == NULL) {
  564. printk("%s: buffer allocation failed\n", tape->name);
  565. goto out;
  566. }
  567. file->private_data = tape;
  568. mutex_unlock(&pt_mutex);
  569. return 0;
  570. out:
  571. atomic_inc(&tape->available);
  572. mutex_unlock(&pt_mutex);
  573. return err;
  574. }
  575. static long pt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  576. {
  577. struct pt_unit *tape = file->private_data;
  578. struct mtop __user *p = (void __user *)arg;
  579. struct mtop mtop;
  580. switch (cmd) {
  581. case MTIOCTOP:
  582. if (copy_from_user(&mtop, p, sizeof(struct mtop)))
  583. return -EFAULT;
  584. switch (mtop.mt_op) {
  585. case MTREW:
  586. mutex_lock(&pt_mutex);
  587. pt_rewind(tape);
  588. mutex_unlock(&pt_mutex);
  589. return 0;
  590. case MTWEOF:
  591. mutex_lock(&pt_mutex);
  592. pt_write_fm(tape);
  593. mutex_unlock(&pt_mutex);
  594. return 0;
  595. default:
  596. /* FIXME: rate limit ?? */
  597. printk(KERN_DEBUG "%s: Unimplemented mt_op %d\n", tape->name,
  598. mtop.mt_op);
  599. return -EINVAL;
  600. }
  601. default:
  602. return -ENOTTY;
  603. }
  604. }
  605. static int
  606. pt_release(struct inode *inode, struct file *file)
  607. {
  608. struct pt_unit *tape = file->private_data;
  609. if (atomic_read(&tape->available) > 1)
  610. return -EINVAL;
  611. if (tape->flags & PT_WRITING)
  612. pt_write_fm(tape);
  613. if (tape->flags & PT_REWIND)
  614. pt_rewind(tape);
  615. kfree(tape->bufptr);
  616. tape->bufptr = NULL;
  617. atomic_inc(&tape->available);
  618. return 0;
  619. }
  620. static ssize_t pt_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
  621. {
  622. struct pt_unit *tape = filp->private_data;
  623. struct pi_adapter *pi = tape->pi;
  624. char rd_cmd[12] = { ATAPI_READ_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  625. int k, n, r, p, s, t, b;
  626. if (!(tape->flags & (PT_READING | PT_WRITING))) {
  627. tape->flags |= PT_READING;
  628. if (pt_atapi(tape, rd_cmd, 0, NULL, "start read-ahead"))
  629. return -EIO;
  630. } else if (tape->flags & PT_WRITING)
  631. return -EIO;
  632. if (tape->flags & PT_EOF)
  633. return 0;
  634. t = 0;
  635. while (count > 0) {
  636. if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "read"))
  637. return -EIO;
  638. n = count;
  639. if (n > 32768)
  640. n = 32768; /* max per command */
  641. b = (n - 1 + tape->bs) / tape->bs;
  642. n = b * tape->bs; /* rounded up to even block */
  643. rd_cmd[4] = b;
  644. r = pt_command(tape, rd_cmd, n, "read");
  645. mdelay(1);
  646. if (r) {
  647. pt_req_sense(tape, 0);
  648. return -EIO;
  649. }
  650. while (1) {
  651. r = pt_wait(tape, STAT_BUSY,
  652. STAT_DRQ | STAT_ERR | STAT_READY,
  653. DBMSG("read DRQ"), "");
  654. if (r & STAT_SENSE) {
  655. pi_disconnect(pi);
  656. pt_req_sense(tape, 0);
  657. return -EIO;
  658. }
  659. if (r)
  660. tape->flags |= PT_EOF;
  661. s = read_reg(pi, 7);
  662. if (!(s & STAT_DRQ))
  663. break;
  664. n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
  665. p = (read_reg(pi, 2) & 3);
  666. if (p != 2) {
  667. pi_disconnect(pi);
  668. printk("%s: Phase error on read: %d\n", tape->name,
  669. p);
  670. return -EIO;
  671. }
  672. while (n > 0) {
  673. k = n;
  674. if (k > PT_BUFSIZE)
  675. k = PT_BUFSIZE;
  676. pi_read_block(pi, tape->bufptr, k);
  677. n -= k;
  678. b = k;
  679. if (b > count)
  680. b = count;
  681. if (copy_to_user(buf + t, tape->bufptr, b)) {
  682. pi_disconnect(pi);
  683. return -EFAULT;
  684. }
  685. t += b;
  686. count -= b;
  687. }
  688. }
  689. pi_disconnect(pi);
  690. if (tape->flags & PT_EOF)
  691. break;
  692. }
  693. return t;
  694. }
  695. static ssize_t pt_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
  696. {
  697. struct pt_unit *tape = filp->private_data;
  698. struct pi_adapter *pi = tape->pi;
  699. char wr_cmd[12] = { ATAPI_WRITE_6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  700. int k, n, r, p, s, t, b;
  701. if (!(tape->flags & PT_WRITE_OK))
  702. return -EROFS;
  703. if (!(tape->flags & (PT_READING | PT_WRITING))) {
  704. tape->flags |= PT_WRITING;
  705. if (pt_atapi
  706. (tape, wr_cmd, 0, NULL, "start buffer-available mode"))
  707. return -EIO;
  708. } else if (tape->flags & PT_READING)
  709. return -EIO;
  710. if (tape->flags & PT_EOF)
  711. return -ENOSPC;
  712. t = 0;
  713. while (count > 0) {
  714. if (!pt_poll_dsc(tape, HZ / 100, PT_TMO, "write"))
  715. return -EIO;
  716. n = count;
  717. if (n > 32768)
  718. n = 32768; /* max per command */
  719. b = (n - 1 + tape->bs) / tape->bs;
  720. n = b * tape->bs; /* rounded up to even block */
  721. wr_cmd[4] = b;
  722. r = pt_command(tape, wr_cmd, n, "write");
  723. mdelay(1);
  724. if (r) { /* error delivering command only */
  725. pt_req_sense(tape, 0);
  726. return -EIO;
  727. }
  728. while (1) {
  729. r = pt_wait(tape, STAT_BUSY,
  730. STAT_DRQ | STAT_ERR | STAT_READY,
  731. DBMSG("write DRQ"), NULL);
  732. if (r & STAT_SENSE) {
  733. pi_disconnect(pi);
  734. pt_req_sense(tape, 0);
  735. return -EIO;
  736. }
  737. if (r)
  738. tape->flags |= PT_EOF;
  739. s = read_reg(pi, 7);
  740. if (!(s & STAT_DRQ))
  741. break;
  742. n = (read_reg(pi, 4) + 256 * read_reg(pi, 5));
  743. p = (read_reg(pi, 2) & 3);
  744. if (p != 0) {
  745. pi_disconnect(pi);
  746. printk("%s: Phase error on write: %d \n",
  747. tape->name, p);
  748. return -EIO;
  749. }
  750. while (n > 0) {
  751. k = n;
  752. if (k > PT_BUFSIZE)
  753. k = PT_BUFSIZE;
  754. b = k;
  755. if (b > count)
  756. b = count;
  757. if (copy_from_user(tape->bufptr, buf + t, b)) {
  758. pi_disconnect(pi);
  759. return -EFAULT;
  760. }
  761. pi_write_block(pi, tape->bufptr, k);
  762. t += b;
  763. count -= b;
  764. n -= k;
  765. }
  766. }
  767. pi_disconnect(pi);
  768. if (tape->flags & PT_EOF)
  769. break;
  770. }
  771. return t;
  772. }
  773. static int __init pt_init(void)
  774. {
  775. int unit;
  776. int err;
  777. if (disable) {
  778. err = -EINVAL;
  779. goto out;
  780. }
  781. if (pt_detect()) {
  782. err = -ENODEV;
  783. goto out;
  784. }
  785. err = register_chrdev(major, name, &pt_fops);
  786. if (err < 0) {
  787. printk("pt_init: unable to get major number %d\n", major);
  788. for (unit = 0; unit < PT_UNITS; unit++)
  789. if (pt[unit].present)
  790. pi_release(pt[unit].pi);
  791. goto out;
  792. }
  793. major = err;
  794. pt_class = class_create(THIS_MODULE, "pt");
  795. if (IS_ERR(pt_class)) {
  796. err = PTR_ERR(pt_class);
  797. goto out_chrdev;
  798. }
  799. for (unit = 0; unit < PT_UNITS; unit++)
  800. if (pt[unit].present) {
  801. device_create(pt_class, NULL, MKDEV(major, unit), NULL,
  802. "pt%d", unit);
  803. device_create(pt_class, NULL, MKDEV(major, unit + 128),
  804. NULL, "pt%dn", unit);
  805. }
  806. goto out;
  807. out_chrdev:
  808. unregister_chrdev(major, "pt");
  809. out:
  810. return err;
  811. }
  812. static void __exit pt_exit(void)
  813. {
  814. int unit;
  815. for (unit = 0; unit < PT_UNITS; unit++)
  816. if (pt[unit].present) {
  817. device_destroy(pt_class, MKDEV(major, unit));
  818. device_destroy(pt_class, MKDEV(major, unit + 128));
  819. }
  820. class_destroy(pt_class);
  821. unregister_chrdev(major, name);
  822. for (unit = 0; unit < PT_UNITS; unit++)
  823. if (pt[unit].present)
  824. pi_release(pt[unit].pi);
  825. }
  826. MODULE_LICENSE("GPL");
  827. module_init(pt_init)
  828. module_exit(pt_exit)