Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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