Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | pg.c (c) 1998 Grant R. Guenther <grant@torque.net> | |
3 | Under the terms of the GNU General Public License. | |
4 | ||
5 | The pg driver provides a simple character device interface for | |
6 | sending ATAPI commands to a device. With the exception of the | |
7 | ATAPI reset operation, all operations are performed by a pair | |
8 | of read and write operations to the appropriate /dev/pgN device. | |
9 | A write operation delivers a command and any outbound data in | |
10 | a single buffer. Normally, the write will succeed unless the | |
11 | device is offline or malfunctioning, or there is already another | |
12 | command pending. If the write succeeds, it should be followed | |
13 | immediately by a read operation, to obtain any returned data and | |
14 | status information. A read will fail if there is no operation | |
15 | in progress. | |
16 | ||
17 | As a special case, the device can be reset with a write operation, | |
18 | and in this case, no following read is expected, or permitted. | |
19 | ||
20 | There are no ioctl() operations. Any single operation | |
21 | may transfer at most PG_MAX_DATA bytes. Note that the driver must | |
22 | copy the data through an internal buffer. In keeping with all | |
23 | current ATAPI devices, command packets are assumed to be exactly | |
24 | 12 bytes in length. | |
25 | ||
26 | To permit future changes to this interface, the headers in the | |
27 | read and write buffers contain a single character "magic" flag. | |
28 | Currently this flag must be the character "P". | |
29 | ||
30 | By default, the driver will autoprobe for a single parallel | |
31 | port ATAPI device, but if their individual parameters are | |
32 | specified, the driver can handle up to 4 devices. | |
33 | ||
34 | To use this device, you must have the following device | |
35 | special files defined: | |
36 | ||
37 | /dev/pg0 c 97 0 | |
38 | /dev/pg1 c 97 1 | |
39 | /dev/pg2 c 97 2 | |
40 | /dev/pg3 c 97 3 | |
41 | ||
42 | (You'll need to change the 97 to something else if you use | |
43 | the 'major' parameter to install the driver on a different | |
44 | major number.) | |
45 | ||
46 | The behaviour of the pg driver can be altered by setting | |
47 | some parameters from the insmod command line. The following | |
48 | parameters are adjustable: | |
49 | ||
50 | drive0 These four arguments can be arrays of | |
51 | drive1 1-6 integers as follows: | |
52 | drive2 | |
53 | drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<dly> | |
54 | ||
55 | Where, | |
56 | ||
57 | <prt> is the base of the parallel port address for | |
58 | the corresponding drive. (required) | |
59 | ||
60 | <pro> is the protocol number for the adapter that | |
61 | supports this drive. These numbers are | |
62 | logged by 'paride' when the protocol modules | |
63 | are initialised. (0 if not given) | |
64 | ||
65 | <uni> for those adapters that support chained | |
66 | devices, this is the unit selector for the | |
67 | chain of devices on the given port. It should | |
68 | be zero for devices that don't support chaining. | |
69 | (0 if not given) | |
70 | ||
71 | <mod> this can be -1 to choose the best mode, or one | |
72 | of the mode numbers supported by the adapter. | |
73 | (-1 if not given) | |
74 | ||
75 | <slv> ATAPI devices can be jumpered to master or slave. | |
76 | Set this to 0 to choose the master drive, 1 to | |
77 | choose the slave, -1 (the default) to choose the | |
78 | first drive found. | |
79 | ||
80 | <dly> some parallel ports require the driver to | |
81 | go more slowly. -1 sets a default value that | |
82 | should work with the chosen protocol. Otherwise, | |
83 | set this to a small integer, the larger it is | |
84 | the slower the port i/o. In some cases, setting | |
85 | this to zero will speed up the device. (default -1) | |
86 | ||
87 | major You may use this parameter to overide the | |
88 | default major number (97) that this driver | |
89 | will use. Be sure to change the device | |
90 | name as well. | |
91 | ||
92 | name This parameter is a character string that | |
93 | contains the name the kernel will use for this | |
94 | device (in /proc output, for instance). | |
95 | (default "pg"). | |
96 | ||
97 | verbose This parameter controls the amount of logging | |
98 | that is done by the driver. Set it to 0 for | |
99 | quiet operation, to 1 to enable progress | |
100 | messages while the driver probes for devices, | |
101 | or to 2 for full debug logging. (default 0) | |
102 | ||
103 | If this driver is built into the kernel, you can use | |
104 | the following command line parameters, with the same values | |
105 | as the corresponding module parameters listed above: | |
106 | ||
107 | pg.drive0 | |
108 | pg.drive1 | |
109 | pg.drive2 | |
110 | pg.drive3 | |
111 | ||
112 | In addition, you can use the parameter pg.disable to disable | |
113 | the driver entirely. | |
114 | ||
115 | */ | |
116 | ||
117 | /* Changes: | |
118 | ||
119 | 1.01 GRG 1998.06.16 Bug fixes | |
120 | 1.02 GRG 1998.09.24 Added jumbo support | |
121 | ||
122 | */ | |
123 | ||
124 | #define PG_VERSION "1.02" | |
125 | #define PG_MAJOR 97 | |
126 | #define PG_NAME "pg" | |
127 | #define PG_UNITS 4 | |
128 | ||
129 | #ifndef PI_PG | |
130 | #define PI_PG 4 | |
131 | #endif | |
132 | ||
133 | /* Here are things one can override from the insmod command. | |
134 | Most are autoprobed by paride unless set here. Verbose is 0 | |
135 | by default. | |
136 | ||
137 | */ | |
138 | ||
139 | static int verbose = 0; | |
140 | static int major = PG_MAJOR; | |
141 | static char *name = PG_NAME; | |
142 | static int disable = 0; | |
143 | ||
144 | static int drive0[6] = { 0, 0, 0, -1, -1, -1 }; | |
145 | static int drive1[6] = { 0, 0, 0, -1, -1, -1 }; | |
146 | static int drive2[6] = { 0, 0, 0, -1, -1, -1 }; | |
147 | static int drive3[6] = { 0, 0, 0, -1, -1, -1 }; | |
148 | ||
149 | static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3}; | |
150 | static int pg_drive_count; | |
151 | ||
152 | enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY}; | |
153 | ||
154 | /* end of parameters */ | |
155 | ||
156 | #include <linux/module.h> | |
157 | #include <linux/init.h> | |
158 | #include <linux/fs.h> | |
1da177e4 LT |
159 | #include <linux/delay.h> |
160 | #include <linux/slab.h> | |
161 | #include <linux/mtio.h> | |
162 | #include <linux/pg.h> | |
163 | #include <linux/device.h> | |
4e57b681 | 164 | #include <linux/sched.h> /* current, TASK_* */ |
ea2959a2 | 165 | #include <linux/smp_lock.h> |
4e57b681 | 166 | #include <linux/jiffies.h> |
1da177e4 LT |
167 | |
168 | #include <asm/uaccess.h> | |
169 | ||
170 | module_param(verbose, bool, 0644); | |
171 | module_param(major, int, 0); | |
172 | module_param(name, charp, 0); | |
173 | module_param_array(drive0, int, NULL, 0); | |
174 | module_param_array(drive1, int, NULL, 0); | |
175 | module_param_array(drive2, int, NULL, 0); | |
176 | module_param_array(drive3, int, NULL, 0); | |
177 | ||
178 | #include "paride.h" | |
179 | ||
180 | #define PG_SPIN_DEL 50 /* spin delay in micro-seconds */ | |
181 | #define PG_SPIN 200 | |
182 | #define PG_TMO HZ | |
183 | #define PG_RESET_TMO 10*HZ | |
184 | ||
185 | #define STAT_ERR 0x01 | |
186 | #define STAT_INDEX 0x02 | |
187 | #define STAT_ECC 0x04 | |
188 | #define STAT_DRQ 0x08 | |
189 | #define STAT_SEEK 0x10 | |
190 | #define STAT_WRERR 0x20 | |
191 | #define STAT_READY 0x40 | |
192 | #define STAT_BUSY 0x80 | |
193 | ||
194 | #define ATAPI_IDENTIFY 0x12 | |
195 | ||
196 | static int pg_open(struct inode *inode, struct file *file); | |
197 | static int pg_release(struct inode *inode, struct file *file); | |
198 | static ssize_t pg_read(struct file *filp, char __user *buf, | |
199 | size_t count, loff_t * ppos); | |
200 | static ssize_t pg_write(struct file *filp, const char __user *buf, | |
201 | size_t count, loff_t * ppos); | |
202 | static int pg_detect(void); | |
203 | ||
204 | #define PG_NAMELEN 8 | |
205 | ||
206 | struct pg { | |
207 | struct pi_adapter pia; /* interface to paride layer */ | |
208 | struct pi_adapter *pi; | |
209 | int busy; /* write done, read expected */ | |
210 | int start; /* jiffies at command start */ | |
211 | int dlen; /* transfer size requested */ | |
212 | unsigned long timeout; /* timeout requested */ | |
213 | int status; /* last sense key */ | |
214 | int drive; /* drive */ | |
215 | unsigned long access; /* count of active opens ... */ | |
216 | int present; /* device present ? */ | |
217 | char *bufptr; | |
218 | char name[PG_NAMELEN]; /* pg0, pg1, ... */ | |
219 | }; | |
220 | ||
221 | static struct pg devices[PG_UNITS]; | |
222 | ||
223 | static int pg_identify(struct pg *dev, int log); | |
224 | ||
225 | static char pg_scratch[512]; /* scratch block buffer */ | |
226 | ||
deb36970 | 227 | static struct class *pg_class; |
1da177e4 LT |
228 | |
229 | /* kernel glue structures */ | |
230 | ||
2b8693c0 | 231 | static const struct file_operations pg_fops = { |
1da177e4 LT |
232 | .owner = THIS_MODULE, |
233 | .read = pg_read, | |
234 | .write = pg_write, | |
235 | .open = pg_open, | |
236 | .release = pg_release, | |
237 | }; | |
238 | ||
239 | static void pg_init_units(void) | |
240 | { | |
241 | int unit; | |
242 | ||
243 | pg_drive_count = 0; | |
244 | for (unit = 0; unit < PG_UNITS; unit++) { | |
245 | int *parm = *drives[unit]; | |
246 | struct pg *dev = &devices[unit]; | |
247 | dev->pi = &dev->pia; | |
248 | clear_bit(0, &dev->access); | |
249 | dev->busy = 0; | |
250 | dev->present = 0; | |
251 | dev->bufptr = NULL; | |
252 | dev->drive = parm[D_SLV]; | |
253 | snprintf(dev->name, PG_NAMELEN, "%s%c", name, 'a'+unit); | |
254 | if (parm[D_PRT]) | |
255 | pg_drive_count++; | |
256 | } | |
257 | } | |
258 | ||
259 | static inline int status_reg(struct pg *dev) | |
260 | { | |
261 | return pi_read_regr(dev->pi, 1, 6); | |
262 | } | |
263 | ||
264 | static inline int read_reg(struct pg *dev, int reg) | |
265 | { | |
266 | return pi_read_regr(dev->pi, 0, reg); | |
267 | } | |
268 | ||
269 | static inline void write_reg(struct pg *dev, int reg, int val) | |
270 | { | |
271 | pi_write_regr(dev->pi, 0, reg, val); | |
272 | } | |
273 | ||
274 | static inline u8 DRIVE(struct pg *dev) | |
275 | { | |
276 | return 0xa0+0x10*dev->drive; | |
277 | } | |
278 | ||
279 | static void pg_sleep(int cs) | |
280 | { | |
86e84862 | 281 | schedule_timeout_interruptible(cs); |
1da177e4 LT |
282 | } |
283 | ||
284 | static int pg_wait(struct pg *dev, int go, int stop, unsigned long tmo, char *msg) | |
285 | { | |
286 | int j, r, e, s, p, to; | |
287 | ||
288 | dev->status = 0; | |
289 | ||
290 | j = 0; | |
291 | while ((((r = status_reg(dev)) & go) || (stop && (!(r & stop)))) | |
292 | && time_before(jiffies, tmo)) { | |
293 | if (j++ < PG_SPIN) | |
294 | udelay(PG_SPIN_DEL); | |
295 | else | |
296 | pg_sleep(1); | |
297 | } | |
298 | ||
299 | to = time_after_eq(jiffies, tmo); | |
300 | ||
301 | if ((r & (STAT_ERR & stop)) || to) { | |
302 | s = read_reg(dev, 7); | |
303 | e = read_reg(dev, 1); | |
304 | p = read_reg(dev, 2); | |
305 | if (verbose > 1) | |
306 | printk("%s: %s: stat=0x%x err=0x%x phase=%d%s\n", | |
307 | dev->name, msg, s, e, p, to ? " timeout" : ""); | |
308 | if (to) | |
309 | e |= 0x100; | |
310 | dev->status = (e >> 4) & 0xff; | |
311 | return -1; | |
312 | } | |
313 | return 0; | |
314 | } | |
315 | ||
316 | static int pg_command(struct pg *dev, char *cmd, int dlen, unsigned long tmo) | |
317 | { | |
318 | int k; | |
319 | ||
320 | pi_connect(dev->pi); | |
321 | ||
322 | write_reg(dev, 6, DRIVE(dev)); | |
323 | ||
324 | if (pg_wait(dev, STAT_BUSY | STAT_DRQ, 0, tmo, "before command")) | |
325 | goto fail; | |
326 | ||
327 | write_reg(dev, 4, dlen % 256); | |
328 | write_reg(dev, 5, dlen / 256); | |
329 | write_reg(dev, 7, 0xa0); /* ATAPI packet command */ | |
330 | ||
331 | if (pg_wait(dev, STAT_BUSY, STAT_DRQ, tmo, "command DRQ")) | |
332 | goto fail; | |
333 | ||
334 | if (read_reg(dev, 2) != 1) { | |
335 | printk("%s: command phase error\n", dev->name); | |
336 | goto fail; | |
337 | } | |
338 | ||
339 | pi_write_block(dev->pi, cmd, 12); | |
340 | ||
341 | if (verbose > 1) { | |
342 | printk("%s: Command sent, dlen=%d packet= ", dev->name, dlen); | |
343 | for (k = 0; k < 12; k++) | |
344 | printk("%02x ", cmd[k] & 0xff); | |
345 | printk("\n"); | |
346 | } | |
347 | return 0; | |
348 | fail: | |
349 | pi_disconnect(dev->pi); | |
350 | return -1; | |
351 | } | |
352 | ||
353 | static int pg_completion(struct pg *dev, char *buf, unsigned long tmo) | |
354 | { | |
355 | int r, d, n, p; | |
356 | ||
357 | r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR, | |
358 | tmo, "completion"); | |
359 | ||
360 | dev->dlen = 0; | |
361 | ||
362 | while (read_reg(dev, 7) & STAT_DRQ) { | |
363 | d = (read_reg(dev, 4) + 256 * read_reg(dev, 5)); | |
364 | n = ((d + 3) & 0xfffc); | |
365 | p = read_reg(dev, 2) & 3; | |
366 | if (p == 0) | |
367 | pi_write_block(dev->pi, buf, n); | |
368 | if (p == 2) | |
369 | pi_read_block(dev->pi, buf, n); | |
370 | if (verbose > 1) | |
371 | printk("%s: %s %d bytes\n", dev->name, | |
372 | p ? "Read" : "Write", n); | |
373 | dev->dlen += (1 - p) * d; | |
374 | buf += d; | |
375 | r = pg_wait(dev, STAT_BUSY, STAT_DRQ | STAT_READY | STAT_ERR, | |
376 | tmo, "completion"); | |
377 | } | |
378 | ||
379 | pi_disconnect(dev->pi); | |
380 | ||
381 | return r; | |
382 | } | |
383 | ||
384 | static int pg_reset(struct pg *dev) | |
385 | { | |
386 | int i, k, err; | |
387 | int expect[5] = { 1, 1, 1, 0x14, 0xeb }; | |
388 | int got[5]; | |
389 | ||
390 | pi_connect(dev->pi); | |
391 | write_reg(dev, 6, DRIVE(dev)); | |
392 | write_reg(dev, 7, 8); | |
393 | ||
394 | pg_sleep(20 * HZ / 1000); | |
395 | ||
396 | k = 0; | |
397 | while ((k++ < PG_RESET_TMO) && (status_reg(dev) & STAT_BUSY)) | |
398 | pg_sleep(1); | |
399 | ||
400 | for (i = 0; i < 5; i++) | |
401 | got[i] = read_reg(dev, i + 1); | |
402 | ||
403 | err = memcmp(expect, got, sizeof(got)) ? -1 : 0; | |
404 | ||
405 | if (verbose) { | |
406 | printk("%s: Reset (%d) signature = ", dev->name, k); | |
407 | for (i = 0; i < 5; i++) | |
408 | printk("%3x", got[i]); | |
409 | if (err) | |
410 | printk(" (incorrect)"); | |
411 | printk("\n"); | |
412 | } | |
413 | ||
414 | pi_disconnect(dev->pi); | |
415 | return err; | |
416 | } | |
417 | ||
418 | static void xs(char *buf, char *targ, int len) | |
419 | { | |
420 | char l = '\0'; | |
421 | int k; | |
422 | ||
423 | for (k = 0; k < len; k++) { | |
424 | char c = *buf++; | |
c8cbec6b | 425 | if (c != ' ' && c != l) |
1da177e4 LT |
426 | l = *targ++ = c; |
427 | } | |
428 | if (l == ' ') | |
429 | targ--; | |
430 | *targ = '\0'; | |
431 | } | |
432 | ||
433 | static int pg_identify(struct pg *dev, int log) | |
434 | { | |
435 | int s; | |
436 | char *ms[2] = { "master", "slave" }; | |
437 | char mf[10], id[18]; | |
438 | char id_cmd[12] = { ATAPI_IDENTIFY, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 }; | |
439 | char buf[36]; | |
440 | ||
441 | s = pg_command(dev, id_cmd, 36, jiffies + PG_TMO); | |
442 | if (s) | |
443 | return -1; | |
444 | s = pg_completion(dev, buf, jiffies + PG_TMO); | |
445 | if (s) | |
446 | return -1; | |
447 | ||
448 | if (log) { | |
449 | xs(buf + 8, mf, 8); | |
450 | xs(buf + 16, id, 16); | |
451 | printk("%s: %s %s, %s\n", dev->name, mf, id, ms[dev->drive]); | |
452 | } | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
457 | /* | |
458 | * returns 0, with id set if drive is detected | |
459 | * -1, if drive detection failed | |
460 | */ | |
461 | static int pg_probe(struct pg *dev) | |
462 | { | |
463 | if (dev->drive == -1) { | |
464 | for (dev->drive = 0; dev->drive <= 1; dev->drive++) | |
465 | if (!pg_reset(dev)) | |
466 | return pg_identify(dev, 1); | |
467 | } else { | |
468 | if (!pg_reset(dev)) | |
469 | return pg_identify(dev, 1); | |
470 | } | |
471 | return -1; | |
472 | } | |
473 | ||
474 | static int pg_detect(void) | |
475 | { | |
476 | struct pg *dev = &devices[0]; | |
477 | int k, unit; | |
478 | ||
479 | printk("%s: %s version %s, major %d\n", name, name, PG_VERSION, major); | |
480 | ||
481 | k = 0; | |
482 | if (pg_drive_count == 0) { | |
483 | if (pi_init(dev->pi, 1, -1, -1, -1, -1, -1, pg_scratch, | |
484 | PI_PG, verbose, dev->name)) { | |
485 | if (!pg_probe(dev)) { | |
486 | dev->present = 1; | |
487 | k++; | |
488 | } else | |
489 | pi_release(dev->pi); | |
490 | } | |
491 | ||
492 | } else | |
493 | for (unit = 0; unit < PG_UNITS; unit++, dev++) { | |
494 | int *parm = *drives[unit]; | |
495 | if (!parm[D_PRT]) | |
496 | continue; | |
497 | if (pi_init(dev->pi, 0, parm[D_PRT], parm[D_MOD], | |
498 | parm[D_UNI], parm[D_PRO], parm[D_DLY], | |
499 | pg_scratch, PI_PG, verbose, dev->name)) { | |
500 | if (!pg_probe(dev)) { | |
501 | dev->present = 1; | |
502 | k++; | |
503 | } else | |
504 | pi_release(dev->pi); | |
505 | } | |
506 | } | |
507 | ||
508 | if (k) | |
509 | return 0; | |
510 | ||
511 | printk("%s: No ATAPI device detected\n", name); | |
512 | return -1; | |
513 | } | |
514 | ||
515 | static int pg_open(struct inode *inode, struct file *file) | |
516 | { | |
517 | int unit = iminor(inode) & 0x7f; | |
518 | struct pg *dev = &devices[unit]; | |
ea2959a2 | 519 | int ret = 0; |
1da177e4 | 520 | |
ea2959a2 JC |
521 | lock_kernel(); |
522 | if ((unit >= PG_UNITS) || (!dev->present)) { | |
523 | ret = -ENODEV; | |
524 | goto out; | |
525 | } | |
1da177e4 | 526 | |
ea2959a2 JC |
527 | if (test_and_set_bit(0, &dev->access)) { |
528 | ret = -EBUSY; | |
529 | goto out; | |
530 | } | |
1da177e4 LT |
531 | |
532 | if (dev->busy) { | |
533 | pg_reset(dev); | |
534 | dev->busy = 0; | |
535 | } | |
536 | ||
537 | pg_identify(dev, (verbose > 1)); | |
538 | ||
539 | dev->bufptr = kmalloc(PG_MAX_DATA, GFP_KERNEL); | |
540 | if (dev->bufptr == NULL) { | |
541 | clear_bit(0, &dev->access); | |
542 | printk("%s: buffer allocation failed\n", dev->name); | |
ea2959a2 JC |
543 | ret = -ENOMEM; |
544 | goto out; | |
1da177e4 LT |
545 | } |
546 | ||
547 | file->private_data = dev; | |
548 | ||
ea2959a2 JC |
549 | out: |
550 | unlock_kernel(); | |
551 | return ret; | |
1da177e4 LT |
552 | } |
553 | ||
554 | static int pg_release(struct inode *inode, struct file *file) | |
555 | { | |
556 | struct pg *dev = file->private_data; | |
557 | ||
558 | kfree(dev->bufptr); | |
559 | dev->bufptr = NULL; | |
560 | clear_bit(0, &dev->access); | |
561 | ||
562 | return 0; | |
563 | } | |
564 | ||
565 | static ssize_t pg_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos) | |
566 | { | |
567 | struct pg *dev = filp->private_data; | |
568 | struct pg_write_hdr hdr; | |
569 | int hs = sizeof (hdr); | |
570 | ||
571 | if (dev->busy) | |
572 | return -EBUSY; | |
573 | if (count < hs) | |
574 | return -EINVAL; | |
575 | ||
576 | if (copy_from_user(&hdr, buf, hs)) | |
577 | return -EFAULT; | |
578 | ||
579 | if (hdr.magic != PG_MAGIC) | |
580 | return -EINVAL; | |
581 | if (hdr.dlen > PG_MAX_DATA) | |
582 | return -EINVAL; | |
583 | if ((count - hs) > PG_MAX_DATA) | |
584 | return -EINVAL; | |
585 | ||
586 | if (hdr.func == PG_RESET) { | |
587 | if (count != hs) | |
588 | return -EINVAL; | |
589 | if (pg_reset(dev)) | |
590 | return -EIO; | |
591 | return count; | |
592 | } | |
593 | ||
594 | if (hdr.func != PG_COMMAND) | |
595 | return -EINVAL; | |
596 | ||
597 | dev->start = jiffies; | |
598 | dev->timeout = hdr.timeout * HZ + HZ / 2 + jiffies; | |
599 | ||
600 | if (pg_command(dev, hdr.packet, hdr.dlen, jiffies + PG_TMO)) { | |
601 | if (dev->status & 0x10) | |
602 | return -ETIME; | |
603 | return -EIO; | |
604 | } | |
605 | ||
606 | dev->busy = 1; | |
607 | ||
608 | if (copy_from_user(dev->bufptr, buf + hs, count - hs)) | |
609 | return -EFAULT; | |
610 | return count; | |
611 | } | |
612 | ||
613 | static ssize_t pg_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | |
614 | { | |
615 | struct pg *dev = filp->private_data; | |
616 | struct pg_read_hdr hdr; | |
617 | int hs = sizeof (hdr); | |
618 | int copy; | |
619 | ||
620 | if (!dev->busy) | |
621 | return -EINVAL; | |
622 | if (count < hs) | |
623 | return -EINVAL; | |
624 | ||
625 | dev->busy = 0; | |
626 | ||
627 | if (pg_completion(dev, dev->bufptr, dev->timeout)) | |
628 | if (dev->status & 0x10) | |
629 | return -ETIME; | |
630 | ||
631 | hdr.magic = PG_MAGIC; | |
632 | hdr.dlen = dev->dlen; | |
633 | copy = 0; | |
634 | ||
635 | if (hdr.dlen < 0) { | |
636 | hdr.dlen = -1 * hdr.dlen; | |
637 | copy = hdr.dlen; | |
638 | if (copy > (count - hs)) | |
639 | copy = count - hs; | |
640 | } | |
641 | ||
642 | hdr.duration = (jiffies - dev->start + HZ / 2) / HZ; | |
643 | hdr.scsi = dev->status & 0x0f; | |
644 | ||
645 | if (copy_to_user(buf, &hdr, hs)) | |
646 | return -EFAULT; | |
647 | if (copy > 0) | |
648 | if (copy_to_user(buf + hs, dev->bufptr, copy)) | |
649 | return -EFAULT; | |
650 | return copy + hs; | |
651 | } | |
652 | ||
653 | static int __init pg_init(void) | |
654 | { | |
8637980b AM |
655 | int unit; |
656 | int err; | |
1da177e4 LT |
657 | |
658 | if (disable){ | |
8bca98ca | 659 | err = -EINVAL; |
1da177e4 LT |
660 | goto out; |
661 | } | |
662 | ||
663 | pg_init_units(); | |
664 | ||
665 | if (pg_detect()) { | |
8bca98ca | 666 | err = -ENODEV; |
1da177e4 LT |
667 | goto out; |
668 | } | |
669 | ||
8637980b AM |
670 | err = register_chrdev(major, name, &pg_fops); |
671 | if (err < 0) { | |
1da177e4 LT |
672 | printk("pg_init: unable to get major number %d\n", major); |
673 | for (unit = 0; unit < PG_UNITS; unit++) { | |
674 | struct pg *dev = &devices[unit]; | |
675 | if (dev->present) | |
676 | pi_release(dev->pi); | |
677 | } | |
1da177e4 LT |
678 | goto out; |
679 | } | |
8637980b | 680 | major = err; /* In case the user specified `major=0' (dynamic) */ |
deb36970 | 681 | pg_class = class_create(THIS_MODULE, "pg"); |
1da177e4 LT |
682 | if (IS_ERR(pg_class)) { |
683 | err = PTR_ERR(pg_class); | |
684 | goto out_chrdev; | |
685 | } | |
1da177e4 LT |
686 | for (unit = 0; unit < PG_UNITS; unit++) { |
687 | struct pg *dev = &devices[unit]; | |
7c69ef79 | 688 | if (dev->present) |
1ff9f542 GKH |
689 | device_create(pg_class, NULL, MKDEV(major, unit), NULL, |
690 | "pg%u", unit); | |
1da177e4 LT |
691 | } |
692 | err = 0; | |
693 | goto out; | |
694 | ||
1da177e4 LT |
695 | out_chrdev: |
696 | unregister_chrdev(major, "pg"); | |
697 | out: | |
698 | return err; | |
699 | } | |
700 | ||
701 | static void __exit pg_exit(void) | |
702 | { | |
703 | int unit; | |
704 | ||
705 | for (unit = 0; unit < PG_UNITS; unit++) { | |
706 | struct pg *dev = &devices[unit]; | |
8ab5e4c1 | 707 | if (dev->present) |
aa275826 | 708 | device_destroy(pg_class, MKDEV(major, unit)); |
1da177e4 | 709 | } |
deb36970 | 710 | class_destroy(pg_class); |
1da177e4 LT |
711 | unregister_chrdev(major, name); |
712 | ||
713 | for (unit = 0; unit < PG_UNITS; unit++) { | |
714 | struct pg *dev = &devices[unit]; | |
715 | if (dev->present) | |
716 | pi_release(dev->pi); | |
717 | } | |
718 | } | |
719 | ||
720 | MODULE_LICENSE("GPL"); | |
721 | module_init(pg_init) | |
722 | module_exit(pg_exit) |