Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2015-2016 Red Hat
0004  * Copyright (C) 2015 Lyude Paul <thatslyude@gmail.com>
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/slab.h>
0009 #include <linux/serio.h>
0010 #include <linux/notifier.h>
0011 #include "rmi_driver.h"
0012 
0013 #define RMI_F03_RX_DATA_OFB     0x01
0014 #define RMI_F03_OB_SIZE         2
0015 
0016 #define RMI_F03_OB_OFFSET       2
0017 #define RMI_F03_OB_DATA_OFFSET      1
0018 #define RMI_F03_OB_FLAG_TIMEOUT     BIT(6)
0019 #define RMI_F03_OB_FLAG_PARITY      BIT(7)
0020 
0021 #define RMI_F03_DEVICE_COUNT        0x07
0022 #define RMI_F03_BYTES_PER_DEVICE    0x07
0023 #define RMI_F03_BYTES_PER_DEVICE_SHIFT  4
0024 #define RMI_F03_QUEUE_LENGTH        0x0F
0025 
0026 #define PSMOUSE_OOB_EXTRA_BTNS      0x01
0027 
0028 struct f03_data {
0029     struct rmi_function *fn;
0030 
0031     struct serio *serio;
0032     bool serio_registered;
0033 
0034     unsigned int overwrite_buttons;
0035 
0036     u8 device_count;
0037     u8 rx_queue_length;
0038 };
0039 
0040 int rmi_f03_overwrite_button(struct rmi_function *fn, unsigned int button,
0041                  int value)
0042 {
0043     struct f03_data *f03 = dev_get_drvdata(&fn->dev);
0044     unsigned int bit;
0045 
0046     if (button < BTN_LEFT || button > BTN_MIDDLE)
0047         return -EINVAL;
0048 
0049     bit = BIT(button - BTN_LEFT);
0050 
0051     if (value)
0052         f03->overwrite_buttons |= bit;
0053     else
0054         f03->overwrite_buttons &= ~bit;
0055 
0056     return 0;
0057 }
0058 
0059 void rmi_f03_commit_buttons(struct rmi_function *fn)
0060 {
0061     struct f03_data *f03 = dev_get_drvdata(&fn->dev);
0062     struct serio *serio = f03->serio;
0063 
0064     serio_pause_rx(serio);
0065     if (serio->drv) {
0066         serio->drv->interrupt(serio, PSMOUSE_OOB_EXTRA_BTNS,
0067                       SERIO_OOB_DATA);
0068         serio->drv->interrupt(serio, f03->overwrite_buttons,
0069                       SERIO_OOB_DATA);
0070     }
0071     serio_continue_rx(serio);
0072 }
0073 
0074 static int rmi_f03_pt_write(struct serio *id, unsigned char val)
0075 {
0076     struct f03_data *f03 = id->port_data;
0077     int error;
0078 
0079     rmi_dbg(RMI_DEBUG_FN, &f03->fn->dev,
0080         "%s: Wrote %.2hhx to PS/2 passthrough address",
0081         __func__, val);
0082 
0083     error = rmi_write(f03->fn->rmi_dev, f03->fn->fd.data_base_addr, val);
0084     if (error) {
0085         dev_err(&f03->fn->dev,
0086             "%s: Failed to write to F03 TX register (%d).\n",
0087             __func__, error);
0088         return error;
0089     }
0090 
0091     return 0;
0092 }
0093 
0094 static int rmi_f03_initialize(struct f03_data *f03)
0095 {
0096     struct rmi_function *fn = f03->fn;
0097     struct device *dev = &fn->dev;
0098     int error;
0099     u8 bytes_per_device;
0100     u8 query1;
0101     u8 query2[RMI_F03_DEVICE_COUNT * RMI_F03_BYTES_PER_DEVICE];
0102     size_t query2_len;
0103 
0104     error = rmi_read(fn->rmi_dev, fn->fd.query_base_addr, &query1);
0105     if (error) {
0106         dev_err(dev, "Failed to read query register (%d).\n", error);
0107         return error;
0108     }
0109 
0110     f03->device_count = query1 & RMI_F03_DEVICE_COUNT;
0111     bytes_per_device = (query1 >> RMI_F03_BYTES_PER_DEVICE_SHIFT) &
0112                 RMI_F03_BYTES_PER_DEVICE;
0113 
0114     query2_len = f03->device_count * bytes_per_device;
0115 
0116     /*
0117      * The first generation of image sensors don't have a second part to
0118      * their f03 query, as such we have to set some of these values manually
0119      */
0120     if (query2_len < 1) {
0121         f03->device_count = 1;
0122         f03->rx_queue_length = 7;
0123     } else {
0124         error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr + 1,
0125                        query2, query2_len);
0126         if (error) {
0127             dev_err(dev,
0128                 "Failed to read second set of query registers (%d).\n",
0129                 error);
0130             return error;
0131         }
0132 
0133         f03->rx_queue_length = query2[0] & RMI_F03_QUEUE_LENGTH;
0134     }
0135 
0136     return 0;
0137 }
0138 
0139 static int rmi_f03_pt_open(struct serio *serio)
0140 {
0141     struct f03_data *f03 = serio->port_data;
0142     struct rmi_function *fn = f03->fn;
0143     const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
0144     const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
0145     u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
0146     int error;
0147 
0148     /*
0149      * Consume any pending data. Some devices like to spam with
0150      * 0xaa 0x00 announcements which may confuse us as we try to
0151      * probe the device.
0152      */
0153     error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
0154     if (!error)
0155         rmi_dbg(RMI_DEBUG_FN, &fn->dev,
0156             "%s: Consumed %*ph (%d) from PS2 guest\n",
0157             __func__, ob_len, obs, ob_len);
0158 
0159     return fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
0160 }
0161 
0162 static void rmi_f03_pt_close(struct serio *serio)
0163 {
0164     struct f03_data *f03 = serio->port_data;
0165     struct rmi_function *fn = f03->fn;
0166 
0167     fn->rmi_dev->driver->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
0168 }
0169 
0170 static int rmi_f03_register_pt(struct f03_data *f03)
0171 {
0172     struct serio *serio;
0173 
0174     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0175     if (!serio)
0176         return -ENOMEM;
0177 
0178     serio->id.type = SERIO_PS_PSTHRU;
0179     serio->write = rmi_f03_pt_write;
0180     serio->open = rmi_f03_pt_open;
0181     serio->close = rmi_f03_pt_close;
0182     serio->port_data = f03;
0183 
0184     strlcpy(serio->name, "RMI4 PS/2 pass-through", sizeof(serio->name));
0185     snprintf(serio->phys, sizeof(serio->phys), "%s/serio0",
0186          dev_name(&f03->fn->dev));
0187     serio->dev.parent = &f03->fn->dev;
0188 
0189     f03->serio = serio;
0190 
0191     printk(KERN_INFO "serio: %s port at %s\n",
0192         serio->name, dev_name(&f03->fn->dev));
0193     serio_register_port(serio);
0194 
0195     return 0;
0196 }
0197 
0198 static int rmi_f03_probe(struct rmi_function *fn)
0199 {
0200     struct device *dev = &fn->dev;
0201     struct f03_data *f03;
0202     int error;
0203 
0204     f03 = devm_kzalloc(dev, sizeof(struct f03_data), GFP_KERNEL);
0205     if (!f03)
0206         return -ENOMEM;
0207 
0208     f03->fn = fn;
0209 
0210     error = rmi_f03_initialize(f03);
0211     if (error < 0)
0212         return error;
0213 
0214     if (f03->device_count != 1)
0215         dev_warn(dev, "found %d devices on PS/2 passthrough",
0216              f03->device_count);
0217 
0218     dev_set_drvdata(dev, f03);
0219     return 0;
0220 }
0221 
0222 static int rmi_f03_config(struct rmi_function *fn)
0223 {
0224     struct f03_data *f03 = dev_get_drvdata(&fn->dev);
0225     int error;
0226 
0227     if (!f03->serio_registered) {
0228         error = rmi_f03_register_pt(f03);
0229         if (error)
0230             return error;
0231 
0232         f03->serio_registered = true;
0233     } else {
0234         /*
0235          * We must be re-configuring the sensor, just enable
0236          * interrupts for this function.
0237          */
0238         fn->rmi_dev->driver->set_irq_bits(fn->rmi_dev, fn->irq_mask);
0239     }
0240 
0241     return 0;
0242 }
0243 
0244 static irqreturn_t rmi_f03_attention(int irq, void *ctx)
0245 {
0246     struct rmi_function *fn = ctx;
0247     struct rmi_device *rmi_dev = fn->rmi_dev;
0248     struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
0249     struct f03_data *f03 = dev_get_drvdata(&fn->dev);
0250     const u16 data_addr = fn->fd.data_base_addr + RMI_F03_OB_OFFSET;
0251     const u8 ob_len = f03->rx_queue_length * RMI_F03_OB_SIZE;
0252     u8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
0253     u8 ob_status;
0254     u8 ob_data;
0255     unsigned int serio_flags;
0256     int i;
0257     int error;
0258 
0259     if (drvdata->attn_data.data) {
0260         /* First grab the data passed by the transport device */
0261         if (drvdata->attn_data.size < ob_len) {
0262             dev_warn(&fn->dev, "F03 interrupted, but data is missing!\n");
0263             return IRQ_HANDLED;
0264         }
0265 
0266         memcpy(obs, drvdata->attn_data.data, ob_len);
0267 
0268         drvdata->attn_data.data += ob_len;
0269         drvdata->attn_data.size -= ob_len;
0270     } else {
0271         /* Grab all of the data registers, and check them for data */
0272         error = rmi_read_block(fn->rmi_dev, data_addr, &obs, ob_len);
0273         if (error) {
0274             dev_err(&fn->dev,
0275                 "%s: Failed to read F03 output buffers: %d\n",
0276                 __func__, error);
0277             serio_interrupt(f03->serio, 0, SERIO_TIMEOUT);
0278             return IRQ_RETVAL(error);
0279         }
0280     }
0281 
0282     for (i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
0283         ob_status = obs[i];
0284         ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];
0285         serio_flags = 0;
0286 
0287         if (!(ob_status & RMI_F03_RX_DATA_OFB))
0288             continue;
0289 
0290         if (ob_status & RMI_F03_OB_FLAG_TIMEOUT)
0291             serio_flags |= SERIO_TIMEOUT;
0292         if (ob_status & RMI_F03_OB_FLAG_PARITY)
0293             serio_flags |= SERIO_PARITY;
0294 
0295         rmi_dbg(RMI_DEBUG_FN, &fn->dev,
0296             "%s: Received %.2hhx from PS2 guest T: %c P: %c\n",
0297             __func__, ob_data,
0298             serio_flags & SERIO_TIMEOUT ?  'Y' : 'N',
0299             serio_flags & SERIO_PARITY ? 'Y' : 'N');
0300 
0301         serio_interrupt(f03->serio, ob_data, serio_flags);
0302     }
0303 
0304     return IRQ_HANDLED;
0305 }
0306 
0307 static void rmi_f03_remove(struct rmi_function *fn)
0308 {
0309     struct f03_data *f03 = dev_get_drvdata(&fn->dev);
0310 
0311     if (f03->serio_registered)
0312         serio_unregister_port(f03->serio);
0313 }
0314 
0315 struct rmi_function_handler rmi_f03_handler = {
0316     .driver = {
0317         .name = "rmi4_f03",
0318     },
0319     .func = 0x03,
0320     .probe = rmi_f03_probe,
0321     .config = rmi_f03_config,
0322     .attention = rmi_f03_attention,
0323     .remove = rmi_f03_remove,
0324 };
0325 
0326 MODULE_AUTHOR("Lyude Paul <thatslyude@gmail.com>");
0327 MODULE_DESCRIPTION("RMI F03 module");
0328 MODULE_LICENSE("GPL");