/* bpmcd.c (c) 1996,1997 Grant R. Guenther Under the terms of the GNU public license. bpmcd.c is a driver for older versions of the MicroSolutions "backpack" CDrom, an external parallel port device. There are two basic versions of the backpack CDrom. Models 160550 and 162550 are version 1 drives and models 163550 and later are version 2. Version 2 models are ATAPI drives while the version 1 models are Mitsumi proprietary (FX001) drives. Both versions use essentially the same parallel port protocol. The 'bpcd' driver supports the version 2 models in Linux, while this 'bpmcd' driver supports the version 1 models. I developed and tested this driver using a model 162550 drive which I borrowed for the purpose. Since I do not have continuing access to that device, and the drives have not been manufactured for several years, this driver is provided as is. I do not know if it will work with all 160550 and 162550 drives. You can compile this driver as a module for any 2.0 kernel, and with a small change it should also work with 2.1. You must ensure that /usr/include/linux and /usr/include/asm are links to the correct include files for the target system. Compile the driver with cc -D__KERNEL__ -DMODULE -O2 -c bpmcd.c You must then load it with insmod. If you are using MODVERSIONS, add the following to the cc command: -DMODVERSIONS -include /usr/include/linux/modversions.h Before attempting to access the new driver, you will need to create a new device special file. The following commands will do that for you: mknod /dev/bpmcd b 41 0 chown root:disk /dev/bpmcd chmod 660 /dev/bpmcd Afterward, you can mount a disk in the usual way: mount -t iso9660 /dev/bpmcd /cdrom (assuming you have made a directory /cdrom to use as a mount point). The device number 41 used above is defined as BPMCD_MAJOR in the code below. This is the device number reserved for the bpcd driver - so should not have a conflict. The driver will attempt to detect which parallel port your backpack is connected to. If this fails for any reason, you can override it by specifying a port on the LILO command line (for built in drivers) or the insmod command (for drivers built as modules). If your drive is on the port at 0x3bc, you would use one of these commands: LILO: bpmcd=0x3bc insmod: insmod bpmcd bp_base=0x3bc The drive will also detect the appropriate transfer mode. If necessary, you can force it to use a specific mode by setting the variable bp_mode on the insmod or LILO command. Mode 0 uses 4-bit reads, mode 1 is the standard bi-directional mode and EPP ports use mode 2. To force the driver to use 4-bit mode, you would specify LILO: bpmcd=0x3bc,0 insmod: insmod bpmcd bp_base=0x3bc bp_mode=0 (you must specify the correct port address if you use this method.) If you are loading bpmcd as a module, you may also specify bp_debug=1 and the driver will log verbose messages as it probes for the appropriate port and mode. If you have problems loading the driver, please include this output with your report. MicroSolutions' protocol allows for several drives to be chained together off the same parallel port. Currently, this driver will recognise only one of them. If you do have more than one drive, it will choose the one with the lowest id number, where the id number is the last two digits of the product's serial number. It is not currently possible to connect a printer to the chained port on the BackPack and expect Linux to use both devices at once. If you need to use this driver together with a printer on the same port, build both the bpmcd and lp drivers as modules. Keep an eye on http://www.torque.net/bpcd.html for news and other information about the driver. If you have any problems with this driver, please send me, grant@torque.net, some mail directly before posting into the newsgroups or mailing lists. */ #define BP_VERSION "0.03" #define BP_BASE 0 /* set to 0 for autoprobe */ #define BP_HOLD 4 /* micro-second port settling time */ #define BP_RETRIES 5 #define BP_DEBUG 0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* change to uaccess.h for 2.1 kernels ... */ #ifndef BPMCD_MAJOR #define BPMCD_MAJOR 41 #endif #define MAJOR_NR BPMCD_MAJOR /* set up defines for blk.h, why don't all drivers do it this way ? */ #define DEVICE_NAME "BackPack" #define DEVICE_REQUEST do_bp_request #define DEVICE_NR(device) (MINOR(device)) #define DEVICE_ON(device) #define DEVICE_OFF(device) #include #define BP_TMO 600 /* timeout in jiffies */ #define BP_DELAY 50 /* spin delay in uS */ #define BP_SPIN (10000/BP_DELAY)*BP_TMO int bpmcd_init(void); void bpmcd_setup(char * str, int * ints); void cleanup_module( void ); static int bp_open(struct inode *inode, struct file *file); static void do_bp_request(void); static void do_bp_read(void); static int bp_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg); static void bp_release (struct inode *inode, struct file *file); static int bp_detect(void); static void bp_lock(void); static void bp_unlock(void); static void bp_eject(void); static void bp_interrupt(void *data); static int bp_base = BP_BASE; static int bp_mode = 0; static int bp_hold = BP_HOLD; static int bp_debug = BP_DEBUG;; static int bp_unit_id; static int bp_found_r0; static int bp_speed; static int bp_retries; static int bp_access = 0; /* count of active opens ... */ static int bp_busy = 0; /* request being processed ? */ static int bp_timeout; /* "interrupt" loop limiter */ static int bp_sector; /* address of next requested sector */ static int bp_count; /* number of blocks still to do */ static char * bp_buf; /* buffer for request in progress */ static char bp_buffer[2048]; /* raw block buffer */ static int bp_bufblk = -1; /* block in buffer, in CD units, -1 for nothing there */ static int nyb_map[256]; /* decodes a nybble */ static int PortCache = 0; /* cache of the control port */ static struct tq_struct bp_tq = {0,0,bp_interrupt,NULL}; /* kernel glue structures */ static struct file_operations bp_fops = { NULL, /* lseek - default */ block_read, /* read - general block-dev read */ block_write, /* write - general block-dev write */ NULL, /* readdir - bad */ NULL, /* select */ bp_ioctl, /* ioctl */ NULL, /* mmap */ bp_open, /* open */ bp_release, /* release */ block_fsync, /* fsync */ NULL, /* fasync */ NULL, /* media change ? */ NULL /* revalidate new media */ }; /* the MicroSolutions protocol uses bits 3,4,5 & 7 of the status port to deliver a nybble in 4 bit mode. We use a table lookup to extract the nybble value. The following function initialises the table. */ static void bp_init_nyb_map( void ) { int i, j; for(i=0;i<256;i++) { j = (i >> 3) & 0x7; if (i & 0x80) j |= 8; nyb_map[i] = j; } } int bpmcd_init (void) /* preliminary initialisation */ { bp_init_nyb_map(); if (bp_detect()) return -1; if (register_blkdev(MAJOR_NR,"bpmcd",&bp_fops)) { printk("bpmcd: unable to get major number %d\n",MAJOR_NR); return -1; } blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST; read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */ return 0; } static int bp_open (struct inode *inode, struct file *file) { if (file->f_mode & 2) return -EROFS; /* wants to write ? */ MOD_INC_USE_COUNT; bp_lock(); bp_access++; return 0; } static void do_bp_request (void) { if (bp_busy) return; while (1) { if ((!CURRENT) || (CURRENT->rq_status == RQ_INACTIVE)) return; INIT_REQUEST; if (CURRENT->cmd == READ) { bp_sector = CURRENT->sector; bp_count = CURRENT->nr_sectors; bp_buf = CURRENT->buffer; do_bp_read(); if (bp_busy) return; } else end_request(0); } } static int bp_ioctl(struct inode *inode,struct file *file, unsigned int cmd, unsigned long arg) /* we currently support only the EJECT ioctl. */ { switch (cmd) { case CDROMEJECT: if (bp_access == 1) { bp_eject(); return 0; } default: return -EINVAL; } } static void bp_release (struct inode *inode, struct file *file) { kdev_t devp; bp_access--; if (!bp_access) { devp = inode->i_rdev; fsync_dev(devp); invalidate_inodes(devp); invalidate_buffers(devp); bp_unlock(); } MOD_DEC_USE_COUNT; } #ifdef MODULE /* Glue for modules ... */ int init_module(void) { int err; long flags; save_flags(flags); cli(); err = bpmcd_init(); restore_flags(flags); return err; } void cleanup_module(void) { long flags; int range; if (bp_mode == 2) range = 8; else range = 3; save_flags(flags); cli(); unregister_blkdev(MAJOR_NR,"bpmcd"); release_region(bp_base,range); restore_flags(flags); } #else /* bpmcd_setup: process lilo command parameters ... syntax: bpmcd=base[,mode[,hold]] */ void bpmcd_setup(char *str, int *ints) { if (ints[0] > 0) bp_base = ints[1]; if (ints[0] > 1) bp_mode = ints[2]; if (ints[0] > 2) bp_hold = ints[3]; } #endif #define out_p(port,byte) outb(byte,bp_base+port);udelay(bp_hold); #define in_p(port) (udelay(bp_hold),inb(bp_base+port)) /* Unlike other PP devices I've worked on, the backpack protocol seems to be driven by *changes* in the values of certain bits on the control port, rather than their absolute value. Hence the unusual macros ... */ #define w0(byte) out_p(0,byte) #define r0() (in_p(0) & 0xff) #define w1(byte) out_p(1,byte) #define r1() (in_p(1) & 0xff) #define r2() (PortCache=(in_p(2) & 0xff)) #define w2(byte) out_p(2,byte) ; PortCache = byte #define t2(pat) PortCache ^= pat; out_p(2,PortCache) #define e2() PortCache &= 0xfe; out_p(2,PortCache) #define o2() PortCache |= 1; out_p(2,PortCache) #define w3(byte) out_p(3,byte); #define w4(byte) out_p(4,byte); #define r4() (in_p(4) & 0xff) static int bp_read_regr( char regr ) { int r, l, h; w0(regr & 0xf); switch (bp_mode) { case 0: w0(regr); t2(2); e2(); t2(4); l = nyb_map[r1()]; t2(4); h = nyb_map[r1()]; return (h << 4) + l; case 1: w0(regr); t2(2); t2(1); t2(0x20); t2(4); r = r0(); t2(1); t2(0x20); return r; case 2: w3(regr); udelay(20); return r4(); } return -1; } static void bp_write_regr( char regr, char val ) { switch (bp_mode) { case 0: case 1: w0(regr); t2(2); w0(val); o2(); t2(4); break; case 2: w3(regr); udelay(20); w4(val); break; } } static void bp_read_data( char * buf, int len ) { int i, l, h; switch (bp_mode) { case 0: w0(0x40); t2(2); e2(); for (i=0;i> 1); } for (j=0;j<2;j++) { v = 0; for (k=0;k<8;k++) { bp_write_regr(6,0xc); bp_write_regr(6,0xd); bp_write_regr(6,0xc); f = bp_read_regr(0); v = 2*v + (f == 0x84); } buf[2*i+1-j] = v; } } bp_write_regr(6,8); bp_write_regr(6,0); bp_write_regr(5,8); bp_disconnect(); if (om == 2) { bp_connect(); bp_write_regr(4,0x18); bp_disconnect(); } bp_mode = om; printk("bpmcd SERIAL: %8.8s\n",&(buf[110])); } #define WR(r,v) bp_write_regr(r,v) #define RR(r) bp_read_regr(r) static int bp_wait( char * fun, char * fun2 ) { int j; j = 0; while ( ((RR(0x41) & 6) == 6) && (j++= BP_SPIN) { printk("bpmcd: %s%s timeout\n",fun,fun2); return -1; } return 0; } static int bp_read_status( char * fun ) { if (bp_wait(fun,", get status")) return -1; if (!(RR(0x41) & 4)) { return RR(0x40); } return -1; } static int bp_get_status( char * fun ) { WR(0x40,0x40); return bp_read_status(fun); } static void bp_wait_ready( void ) { int k; k = 0; while ((!(bp_get_status("Ready wait") & 0x40)) && (k++ < BP_SPIN)) udelay(BP_DELAY); if (k >= BP_SPIN) printk("bpmcd: Drive not ready\n"); } static int bp_check_status( char * fun ) { int s; s = bp_read_status(fun); if (s == -1) return -1; if (s & 7) { printk("bpmcd: %s, error, status: %x\n",fun,s); return -1; } return 0; } static int bp_completion( void ) { int s; s = -1; if (!bp_wait("Read data","")) { if (!(RR(0x41) & 2)) bp_read_data(bp_buffer,2048); s = bp_check_status("Read data"); } bp_disconnect(); return s; } static void bp_lock(void) { bp_connect(); if (bp_get_status("Door check") & 0x80) { WR(0x40,0xf8); bp_check_status("Close door"); } bp_wait_ready(); WR(0x40,0xfe); WR(0x40,1); bp_check_status("Lock door"); bp_disconnect(); } static void bp_unlock( void) { bp_connect(); WR(0x40,0xfe); WR(0x40,0); bp_check_status("Unlock door"); bp_disconnect(); } static void bp_eject( void) { bp_unlock(); bp_connect(); WR(0x40,0xf6); bp_check_status("Open tray"); bp_disconnect(); } static int bp_reset( void ) { long flags; int i, s; bp_connect(); WR(0x41,0); save_flags(flags); sti(); for (i=0;i<40000;i++) RR(0x41); restore_flags(flags); RR(0x40); s = bp_get_status("Reset"); if (bp_debug) printk("bpmcd: Reset status: %x\n",s); bp_disconnect(); return (s & 1); } static int bp_get_info ( void ) { int r, s, t, v; bp_connect(); WR(0x40,0xdc); v = -1; if (!(r=bp_wait("Get drive info",""))) { if (!(RR(0x41) & 4)) { s = RR(0x40); t = RR(0x40); v = RR(0x40); if (bp_debug) printk("bpmcd: Drive info: %x %c %x\n",s,t,v); if (t == 'D') bp_speed = 1; else bp_speed = 0; } } bp_disconnect(); return v; } static int bp_port_check( void ) /* check for 8-bit port */ { int i, r, m; w2(0x2c); i = r0(); w0(255-i); r = r0(); w0(i); m = -1; if (r == i) m = 1; if (r == (255-i)) m = 0; w2(0xc); i = r0(); w0(255-i); r = r0(); w0(i); if (r != (255-i)) m = -1; if (m == 0) { w2(6); w2(0xc); r = r0(); w0(0xaa); w0(r); w0(0xaa); } if (m == 1) { w2(0x26); w2(0xc); } return m; } static int bp_locate( void ) { int k; for(k=0;k<256;k++) if (!bp_check_id(k)) { bp_unit_id = k; return 0; } return -1; } static void bp_probe ( int port, int max_mode ) { int j, best, range; range = 3; if (max_mode == 2) range = 8; if (check_region(port,range)) { max_mode = 1; range = 3; if (check_region(port,range)) return; } bp_base = port; j = bp_port_check(); if (j == -1) { bp_base = 0; return; } if (bp_locate()) { if (bp_debug) printk("bpmcd CHECK: no backpack adapter at 0x%x\n",port); bp_base = 0; return; } best = -1; /* if (j == 0) max_mode = 0; */ if (bp_debug) printk("bpmcd PROBE: port 0x%x, id %d, max_mode %d\n", port, bp_unit_id, max_mode); for (bp_mode=0;bp_mode<=max_mode;bp_mode++) if (!bp_test_proto()) best = bp_mode; bp_mode = best; if (bp_mode < 0) bp_base = 0; } static int bp_detect( void ) { char *mode_str[3] = { "4-bit", "8-bit", "EPP" }; char *speed_str[2] = { "single", "double" }; int range; int vercode; if (!bp_base) bp_probe(0x278,2); if (!bp_base) bp_probe(0x378,2); if (!bp_base) bp_probe(0x3bc,1); if (!bp_base) { printk("bpmcd: autoprobe failed\n"); return -1; } if (bp_mode == 2) range = 8; else range = 3; if (check_region(bp_base,range)) { printk("bpmcd: Ports at 0x%x are not available\n",bp_base); return -1; } if (bp_port_check() < 0) { printk("bpmcd: No parallel port at 0x%x\n",bp_base); return -1; } if (bp_locate()) { printk("bpmcd: Couldn't find a backpack adapter at 0x%x\n", bp_base); return -1; } if (bp_debug) printk("bpmcd ID: unit %d\n",bp_unit_id); if (bp_debug) bp_read_eeprom(); if (bp_test_proto()) { printk("bpmcd: Can't access backpack in %s mode\n", mode_str[bp_mode]); return -1; } printk("bpmcd %s: backpack ID %d, using port 0x%x in %s mode\n", BP_VERSION,bp_unit_id,bp_base,mode_str[bp_mode]); if (bp_reset()) { printk("bpmcd: Failed to reset CD drive\n"); return -1; } if ((vercode=bp_get_info()) == -1) { printk("bpmcd: Failed to identify drive\n"); return -1; } if (bp_mode == 2) range = 8; else range = 3; request_region(bp_base,range,"bpmcd"); printk("bpmcd: Found Mitsumi %s-speed drive, version %x\n", speed_str[bp_speed],vercode); return 0; } static void bp_transfer( void ) { int k, o; while (bp_count && (bp_sector/4 == bp_bufblk)) { o = (bp_sector % 4) * 512; for(k=0;k<512;k++) bp_buf[k] = bp_buffer[o+k]; bp_count--; bp_buf += 512; bp_sector++; } } #define BCD(x) ((x%10)|((x/10)<<4)) static void bp_read( int sector ) { int k; int cmd[7]; bp_connect(); cmd[0] = 0xc0 + bp_speed; k = sector + 150; cmd[1] = BCD(k/4500); k = k % 4500; cmd[2] = BCD(k / 75); cmd[3] = BCD(k % 75); cmd[4] = 0; cmd[5] = 0; cmd[6] = 1; for (k=0;k<7;k++) WR(0x40,cmd[k]); } static void do_bp_read( void ) { bp_busy = 1; bp_retries = 0; bp_transfer(); if (!bp_count) { end_request(1); bp_busy = 0; return; } sti(); bp_bufblk = bp_sector / 4; bp_read( bp_bufblk); bp_timeout = jiffies + BP_TMO; queue_task(&bp_tq,&tq_scheduler); } static void bp_interrupt( void *data) { if ( (RR(0x41) & 6) == 6 ) { if (jiffies > bp_timeout) { bp_bufblk = -1; bp_busy = 0; end_request(0); do_bp_request(); return; } queue_task(&bp_tq,&tq_scheduler); return; } sti(); if (bp_completion()) { if (bp_retries < BP_RETRIES) { udelay(100000); bp_retries++; bp_read( bp_bufblk); bp_timeout = jiffies + BP_TMO; queue_task(&bp_tq,&tq_scheduler); return; } cli(); bp_busy = 0; bp_bufblk = -1; end_request(0); do_bp_request(); return; } do_bp_read(); do_bp_request(); } /* end of bpmcd.c */