1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * HP Human Interface Loop Master Link Controller driver. 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Copyright (c) 2001 Brian S. Julin 5*4882a593Smuzhiyun * All rights reserved. 6*4882a593Smuzhiyun * 7*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without 8*4882a593Smuzhiyun * modification, are permitted provided that the following conditions 9*4882a593Smuzhiyun * are met: 10*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright 11*4882a593Smuzhiyun * notice, this list of conditions, and the following disclaimer, 12*4882a593Smuzhiyun * without modification. 13*4882a593Smuzhiyun * 2. The name of the author may not be used to endorse or promote products 14*4882a593Smuzhiyun * derived from this software without specific prior written permission. 15*4882a593Smuzhiyun * 16*4882a593Smuzhiyun * Alternatively, this software may be distributed under the terms of the 17*4882a593Smuzhiyun * GNU General Public License ("GPL"). 18*4882a593Smuzhiyun * 19*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20*4882a593Smuzhiyun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21*4882a593Smuzhiyun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22*4882a593Smuzhiyun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 23*4882a593Smuzhiyun * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24*4882a593Smuzhiyun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25*4882a593Smuzhiyun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26*4882a593Smuzhiyun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27*4882a593Smuzhiyun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28*4882a593Smuzhiyun * 29*4882a593Smuzhiyun * References: 30*4882a593Smuzhiyun * HP-HIL Technical Reference Manual. Hewlett Packard Product No. 45918A 31*4882a593Smuzhiyun * 32*4882a593Smuzhiyun */ 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun #include <linux/hil.h> 35*4882a593Smuzhiyun #include <linux/time.h> 36*4882a593Smuzhiyun #include <linux/interrupt.h> 37*4882a593Smuzhiyun #include <linux/semaphore.h> 38*4882a593Smuzhiyun #include <linux/serio.h> 39*4882a593Smuzhiyun #include <linux/list.h> 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun typedef struct hil_mlc hil_mlc; 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /* The HIL has a complicated state engine. 44*4882a593Smuzhiyun * We define the structure of nodes in the state engine here. 45*4882a593Smuzhiyun */ 46*4882a593Smuzhiyun enum hilse_act { 47*4882a593Smuzhiyun /* HILSE_OUT prepares to receive input if the next node 48*4882a593Smuzhiyun * is an IN or EXPECT, and then sends the given packet. 49*4882a593Smuzhiyun */ 50*4882a593Smuzhiyun HILSE_OUT = 0, 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun /* HILSE_CTS checks if the loop is busy. */ 53*4882a593Smuzhiyun HILSE_CTS, 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun /* HILSE_OUT_LAST sends the given command packet to 56*4882a593Smuzhiyun * the last configured/running device on the loop. 57*4882a593Smuzhiyun */ 58*4882a593Smuzhiyun HILSE_OUT_LAST, 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun /* HILSE_OUT_DISC sends the given command packet to 61*4882a593Smuzhiyun * the next device past the last configured/running one. 62*4882a593Smuzhiyun */ 63*4882a593Smuzhiyun HILSE_OUT_DISC, 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun /* HILSE_FUNC runs a callback function with given arguments. 66*4882a593Smuzhiyun * a positive return value causes the "ugly" branch to be taken. 67*4882a593Smuzhiyun */ 68*4882a593Smuzhiyun HILSE_FUNC, 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun /* HILSE_IN simply expects any non-errored packet to arrive 71*4882a593Smuzhiyun * within arg usecs. 72*4882a593Smuzhiyun */ 73*4882a593Smuzhiyun HILSE_IN = 0x100, 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun /* HILSE_EXPECT expects a particular packet to arrive 76*4882a593Smuzhiyun * within arg usecs, any other packet is considered an error. 77*4882a593Smuzhiyun */ 78*4882a593Smuzhiyun HILSE_EXPECT, 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun /* HILSE_EXPECT_LAST as above but dev field should be last 81*4882a593Smuzhiyun * discovered/operational device. 82*4882a593Smuzhiyun */ 83*4882a593Smuzhiyun HILSE_EXPECT_LAST, 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun /* HILSE_EXPECT_LAST as above but dev field should be first 86*4882a593Smuzhiyun * undiscovered/inoperational device. 87*4882a593Smuzhiyun */ 88*4882a593Smuzhiyun HILSE_EXPECT_DISC 89*4882a593Smuzhiyun }; 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun typedef int (hilse_func) (hil_mlc *mlc, int arg); 92*4882a593Smuzhiyun struct hilse_node { 93*4882a593Smuzhiyun enum hilse_act act; /* How to process this node */ 94*4882a593Smuzhiyun union { 95*4882a593Smuzhiyun hilse_func *func; /* Function to call if HILSE_FUNC */ 96*4882a593Smuzhiyun hil_packet packet; /* Packet to send or to compare */ 97*4882a593Smuzhiyun } object; 98*4882a593Smuzhiyun int arg; /* Timeout in usec or parm for func */ 99*4882a593Smuzhiyun int good; /* Node to jump to on success */ 100*4882a593Smuzhiyun int bad; /* Node to jump to on error */ 101*4882a593Smuzhiyun int ugly; /* Node to jump to on timeout */ 102*4882a593Smuzhiyun }; 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun /* Methods for back-end drivers, e.g. hp_sdc_mlc */ 105*4882a593Smuzhiyun typedef int (hil_mlc_cts) (hil_mlc *mlc); 106*4882a593Smuzhiyun typedef int (hil_mlc_out) (hil_mlc *mlc); 107*4882a593Smuzhiyun typedef int (hil_mlc_in) (hil_mlc *mlc, suseconds_t timeout); 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun struct hil_mlc_devinfo { 110*4882a593Smuzhiyun uint8_t idd[16]; /* Device ID Byte and Describe Record */ 111*4882a593Smuzhiyun uint8_t rsc[16]; /* Security Code Header and Record */ 112*4882a593Smuzhiyun uint8_t exd[16]; /* Extended Describe Record */ 113*4882a593Smuzhiyun uint8_t rnm[16]; /* Device name as returned by RNM command */ 114*4882a593Smuzhiyun }; 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun struct hil_mlc_serio_map { 117*4882a593Smuzhiyun hil_mlc *mlc; 118*4882a593Smuzhiyun int di_revmap; 119*4882a593Smuzhiyun int didx; 120*4882a593Smuzhiyun }; 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun /* How many (possibly old/detached) devices the we try to keep track of */ 123*4882a593Smuzhiyun #define HIL_MLC_DEVMEM 16 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun struct hil_mlc { 126*4882a593Smuzhiyun struct list_head list; /* hil_mlc is organized as linked list */ 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun rwlock_t lock; 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun void *priv; /* Data specific to a particular type of MLC */ 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun int seidx; /* Current node in state engine */ 133*4882a593Smuzhiyun int istarted, ostarted; 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun hil_mlc_cts *cts; 136*4882a593Smuzhiyun struct semaphore csem; /* Raised when loop idle */ 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun hil_mlc_out *out; 139*4882a593Smuzhiyun struct semaphore osem; /* Raised when outpacket dispatched */ 140*4882a593Smuzhiyun hil_packet opacket; 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun hil_mlc_in *in; 143*4882a593Smuzhiyun struct semaphore isem; /* Raised when a packet arrives */ 144*4882a593Smuzhiyun hil_packet ipacket[16]; 145*4882a593Smuzhiyun hil_packet imatch; 146*4882a593Smuzhiyun int icount; 147*4882a593Smuzhiyun unsigned long instart; 148*4882a593Smuzhiyun unsigned long intimeout; 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun int ddi; /* Last operational device id */ 151*4882a593Smuzhiyun int lcv; /* LCV to throttle loops */ 152*4882a593Smuzhiyun time64_t lcv_time; /* Time loop was started */ 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun int di_map[7]; /* Maps below items to live devs */ 155*4882a593Smuzhiyun struct hil_mlc_devinfo di[HIL_MLC_DEVMEM]; 156*4882a593Smuzhiyun struct serio *serio[HIL_MLC_DEVMEM]; 157*4882a593Smuzhiyun struct hil_mlc_serio_map serio_map[HIL_MLC_DEVMEM]; 158*4882a593Smuzhiyun hil_packet serio_opacket[HIL_MLC_DEVMEM]; 159*4882a593Smuzhiyun int serio_oidx[HIL_MLC_DEVMEM]; 160*4882a593Smuzhiyun struct hil_mlc_devinfo di_scratch; /* Temporary area */ 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun int opercnt; 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun struct tasklet_struct *tasklet; 165*4882a593Smuzhiyun }; 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun int hil_mlc_register(hil_mlc *mlc); 168*4882a593Smuzhiyun int hil_mlc_unregister(hil_mlc *mlc); 169