Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * HID class driver for the Greybus.
0004  *
0005  * Copyright 2014 Google Inc.
0006  * Copyright 2014 Linaro Ltd.
0007  */
0008 
0009 #include <linux/bitops.h>
0010 #include <linux/hid.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/mutex.h>
0014 #include <linux/slab.h>
0015 #include <linux/greybus.h>
0016 
0017 /* Greybus HID device's structure */
0018 struct gb_hid {
0019     struct gb_bundle *bundle;
0020     struct gb_connection        *connection;
0021 
0022     struct hid_device       *hid;
0023     struct gb_hid_desc_response hdesc;
0024 
0025     unsigned long           flags;
0026 #define GB_HID_STARTED          0x01
0027 #define GB_HID_READ_PENDING     0x04
0028 
0029     unsigned int            bufsize;
0030     char                *inbuf;
0031 };
0032 
0033 /* Routines to get controller's information over greybus */
0034 
0035 /* Operations performed on greybus */
0036 static int gb_hid_get_desc(struct gb_hid *ghid)
0037 {
0038     return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
0039                  0, &ghid->hdesc, sizeof(ghid->hdesc));
0040 }
0041 
0042 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
0043 {
0044     int ret;
0045 
0046     ret = gb_pm_runtime_get_sync(ghid->bundle);
0047     if (ret)
0048         return ret;
0049 
0050     ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
0051                 NULL, 0, rdesc,
0052                 le16_to_cpu(ghid->hdesc.wReportDescLength));
0053 
0054     gb_pm_runtime_put_autosuspend(ghid->bundle);
0055 
0056     return ret;
0057 }
0058 
0059 static int gb_hid_set_power(struct gb_hid *ghid, int type)
0060 {
0061     int ret;
0062 
0063     ret = gb_pm_runtime_get_sync(ghid->bundle);
0064     if (ret)
0065         return ret;
0066 
0067     ret = gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
0068 
0069     gb_pm_runtime_put_autosuspend(ghid->bundle);
0070 
0071     return ret;
0072 }
0073 
0074 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
0075                  unsigned char *buf, int len)
0076 {
0077     struct gb_hid_get_report_request request;
0078     int ret;
0079 
0080     ret = gb_pm_runtime_get_sync(ghid->bundle);
0081     if (ret)
0082         return ret;
0083 
0084     request.report_type = report_type;
0085     request.report_id = report_id;
0086 
0087     ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
0088                 &request, sizeof(request), buf, len);
0089 
0090     gb_pm_runtime_put_autosuspend(ghid->bundle);
0091 
0092     return ret;
0093 }
0094 
0095 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
0096                  unsigned char *buf, int len)
0097 {
0098     struct gb_hid_set_report_request *request;
0099     struct gb_operation *operation;
0100     int ret, size = sizeof(*request) + len - 1;
0101 
0102     ret = gb_pm_runtime_get_sync(ghid->bundle);
0103     if (ret)
0104         return ret;
0105 
0106     operation = gb_operation_create(ghid->connection,
0107                     GB_HID_TYPE_SET_REPORT, size, 0,
0108                     GFP_KERNEL);
0109     if (!operation) {
0110         gb_pm_runtime_put_autosuspend(ghid->bundle);
0111         return -ENOMEM;
0112     }
0113 
0114     request = operation->request->payload;
0115     request->report_type = report_type;
0116     request->report_id = report_id;
0117     memcpy(request->report, buf, len);
0118 
0119     ret = gb_operation_request_send_sync(operation);
0120     if (ret) {
0121         dev_err(&operation->connection->bundle->dev,
0122             "failed to set report: %d\n", ret);
0123     } else {
0124         ret = len;
0125     }
0126 
0127     gb_operation_put(operation);
0128     gb_pm_runtime_put_autosuspend(ghid->bundle);
0129 
0130     return ret;
0131 }
0132 
0133 static int gb_hid_request_handler(struct gb_operation *op)
0134 {
0135     struct gb_connection *connection = op->connection;
0136     struct gb_hid *ghid = gb_connection_get_data(connection);
0137     struct gb_hid_input_report_request *request = op->request->payload;
0138 
0139     if (op->type != GB_HID_TYPE_IRQ_EVENT) {
0140         dev_err(&connection->bundle->dev,
0141             "unsupported unsolicited request\n");
0142         return -EINVAL;
0143     }
0144 
0145     if (test_bit(GB_HID_STARTED, &ghid->flags))
0146         hid_input_report(ghid->hid, HID_INPUT_REPORT,
0147                  request->report, op->request->payload_size, 1);
0148 
0149     return 0;
0150 }
0151 
0152 static int gb_hid_report_len(struct hid_report *report)
0153 {
0154     return ((report->size - 1) >> 3) + 1 +
0155         report->device->report_enum[report->type].numbered;
0156 }
0157 
0158 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
0159                    unsigned int *max)
0160 {
0161     struct hid_report *report;
0162     unsigned int size;
0163 
0164     list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
0165         size = gb_hid_report_len(report);
0166         if (*max < size)
0167             *max = size;
0168     }
0169 }
0170 
0171 static void gb_hid_free_buffers(struct gb_hid *ghid)
0172 {
0173     kfree(ghid->inbuf);
0174     ghid->inbuf = NULL;
0175     ghid->bufsize = 0;
0176 }
0177 
0178 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
0179 {
0180     ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
0181     if (!ghid->inbuf)
0182         return -ENOMEM;
0183 
0184     ghid->bufsize = bufsize;
0185 
0186     return 0;
0187 }
0188 
0189 /* Routines dealing with reports */
0190 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
0191 {
0192     unsigned int size;
0193 
0194     size = gb_hid_report_len(report);
0195     if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
0196                   size))
0197         return;
0198 
0199     /*
0200      * hid->driver_lock is held as we are in probe function,
0201      * we just need to setup the input fields, so using
0202      * hid_report_raw_event is safe.
0203      */
0204     hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
0205 }
0206 
0207 static void gb_hid_init_reports(struct gb_hid *ghid)
0208 {
0209     struct hid_device *hid = ghid->hid;
0210     struct hid_report *report;
0211 
0212     list_for_each_entry(report,
0213                 &hid->report_enum[HID_INPUT_REPORT].report_list,
0214                 list)
0215         gb_hid_init_report(ghid, report);
0216 
0217     list_for_each_entry(report,
0218                 &hid->report_enum[HID_FEATURE_REPORT].report_list,
0219                 list)
0220         gb_hid_init_report(ghid, report);
0221 }
0222 
0223 static int __gb_hid_get_raw_report(struct hid_device *hid,
0224                    unsigned char report_number, __u8 *buf, size_t count,
0225                    unsigned char report_type)
0226 {
0227     struct gb_hid *ghid = hid->driver_data;
0228     int ret;
0229 
0230     if (report_type == HID_OUTPUT_REPORT)
0231         return -EINVAL;
0232 
0233     ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
0234     if (!ret)
0235         ret = count;
0236 
0237     return ret;
0238 }
0239 
0240 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
0241                       size_t len, unsigned char report_type)
0242 {
0243     struct gb_hid *ghid = hid->driver_data;
0244     int report_id = buf[0];
0245     int ret;
0246 
0247     if (report_type == HID_INPUT_REPORT)
0248         return -EINVAL;
0249 
0250     if (report_id) {
0251         buf++;
0252         len--;
0253     }
0254 
0255     ret = gb_hid_set_report(ghid, report_type, report_id, buf, len);
0256     if (report_id && ret >= 0)
0257         ret++; /* add report_id to the number of transferred bytes */
0258 
0259     return 0;
0260 }
0261 
0262 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
0263                   __u8 *buf, size_t len, unsigned char rtype,
0264                   int reqtype)
0265 {
0266     switch (reqtype) {
0267     case HID_REQ_GET_REPORT:
0268         return __gb_hid_get_raw_report(hid, reportnum, buf, len, rtype);
0269     case HID_REQ_SET_REPORT:
0270         if (buf[0] != reportnum)
0271             return -EINVAL;
0272         return __gb_hid_output_raw_report(hid, buf, len, rtype);
0273     default:
0274         return -EIO;
0275     }
0276 }
0277 
0278 /* HID Callbacks */
0279 static int gb_hid_parse(struct hid_device *hid)
0280 {
0281     struct gb_hid *ghid = hid->driver_data;
0282     unsigned int rsize;
0283     char *rdesc;
0284     int ret;
0285 
0286     rsize = le16_to_cpu(ghid->hdesc.wReportDescLength);
0287     if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
0288         dbg_hid("weird size of report descriptor (%u)\n", rsize);
0289         return -EINVAL;
0290     }
0291 
0292     rdesc = kzalloc(rsize, GFP_KERNEL);
0293     if (!rdesc)
0294         return -ENOMEM;
0295 
0296     ret = gb_hid_get_report_desc(ghid, rdesc);
0297     if (ret) {
0298         hid_err(hid, "reading report descriptor failed\n");
0299         goto free_rdesc;
0300     }
0301 
0302     ret = hid_parse_report(hid, rdesc, rsize);
0303     if (ret)
0304         dbg_hid("parsing report descriptor failed\n");
0305 
0306 free_rdesc:
0307     kfree(rdesc);
0308 
0309     return ret;
0310 }
0311 
0312 static int gb_hid_start(struct hid_device *hid)
0313 {
0314     struct gb_hid *ghid = hid->driver_data;
0315     unsigned int bufsize = HID_MIN_BUFFER_SIZE;
0316     int ret;
0317 
0318     gb_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
0319     gb_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
0320     gb_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
0321 
0322     if (bufsize > HID_MAX_BUFFER_SIZE)
0323         bufsize = HID_MAX_BUFFER_SIZE;
0324 
0325     ret = gb_hid_alloc_buffers(ghid, bufsize);
0326     if (ret)
0327         return ret;
0328 
0329     if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
0330         gb_hid_init_reports(ghid);
0331 
0332     return 0;
0333 }
0334 
0335 static void gb_hid_stop(struct hid_device *hid)
0336 {
0337     struct gb_hid *ghid = hid->driver_data;
0338 
0339     gb_hid_free_buffers(ghid);
0340 }
0341 
0342 static int gb_hid_open(struct hid_device *hid)
0343 {
0344     struct gb_hid *ghid = hid->driver_data;
0345     int ret;
0346 
0347     ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
0348     if (ret < 0)
0349         return ret;
0350 
0351     set_bit(GB_HID_STARTED, &ghid->flags);
0352     return 0;
0353 }
0354 
0355 static void gb_hid_close(struct hid_device *hid)
0356 {
0357     struct gb_hid *ghid = hid->driver_data;
0358     int ret;
0359 
0360     clear_bit(GB_HID_STARTED, &ghid->flags);
0361 
0362     /* Save some power */
0363     ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
0364     if (ret)
0365         dev_err(&ghid->connection->bundle->dev,
0366             "failed to power off (%d)\n", ret);
0367 }
0368 
0369 static int gb_hid_power(struct hid_device *hid, int lvl)
0370 {
0371     struct gb_hid *ghid = hid->driver_data;
0372 
0373     switch (lvl) {
0374     case PM_HINT_FULLON:
0375         return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
0376     case PM_HINT_NORMAL:
0377         return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
0378     }
0379 
0380     return 0;
0381 }
0382 
0383 /* HID structure to pass callbacks */
0384 static struct hid_ll_driver gb_hid_ll_driver = {
0385     .parse = gb_hid_parse,
0386     .start = gb_hid_start,
0387     .stop = gb_hid_stop,
0388     .open = gb_hid_open,
0389     .close = gb_hid_close,
0390     .power = gb_hid_power,
0391     .raw_request = gb_hid_raw_request,
0392 };
0393 
0394 static int gb_hid_init(struct gb_hid *ghid)
0395 {
0396     struct hid_device *hid = ghid->hid;
0397     int ret;
0398 
0399     ret = gb_hid_get_desc(ghid);
0400     if (ret)
0401         return ret;
0402 
0403     hid->version = le16_to_cpu(ghid->hdesc.bcdHID);
0404     hid->vendor = le16_to_cpu(ghid->hdesc.wVendorID);
0405     hid->product = le16_to_cpu(ghid->hdesc.wProductID);
0406     hid->country = ghid->hdesc.bCountryCode;
0407 
0408     hid->driver_data = ghid;
0409     hid->ll_driver = &gb_hid_ll_driver;
0410     hid->dev.parent = &ghid->connection->bundle->dev;
0411 //  hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
0412 
0413     /* Set HID device's name */
0414     snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
0415          dev_name(&ghid->connection->bundle->dev),
0416          hid->vendor, hid->product);
0417 
0418     return 0;
0419 }
0420 
0421 static int gb_hid_probe(struct gb_bundle *bundle,
0422             const struct greybus_bundle_id *id)
0423 {
0424     struct greybus_descriptor_cport *cport_desc;
0425     struct gb_connection *connection;
0426     struct hid_device *hid;
0427     struct gb_hid *ghid;
0428     int ret;
0429 
0430     if (bundle->num_cports != 1)
0431         return -ENODEV;
0432 
0433     cport_desc = &bundle->cport_desc[0];
0434     if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
0435         return -ENODEV;
0436 
0437     ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
0438     if (!ghid)
0439         return -ENOMEM;
0440 
0441     connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
0442                       gb_hid_request_handler);
0443     if (IS_ERR(connection)) {
0444         ret = PTR_ERR(connection);
0445         goto err_free_ghid;
0446     }
0447 
0448     gb_connection_set_data(connection, ghid);
0449     ghid->connection = connection;
0450 
0451     hid = hid_allocate_device();
0452     if (IS_ERR(hid)) {
0453         ret = PTR_ERR(hid);
0454         goto err_connection_destroy;
0455     }
0456 
0457     ghid->hid = hid;
0458     ghid->bundle = bundle;
0459 
0460     greybus_set_drvdata(bundle, ghid);
0461 
0462     ret = gb_connection_enable(connection);
0463     if (ret)
0464         goto err_destroy_hid;
0465 
0466     ret = gb_hid_init(ghid);
0467     if (ret)
0468         goto err_connection_disable;
0469 
0470     ret = hid_add_device(hid);
0471     if (ret) {
0472         hid_err(hid, "can't add hid device: %d\n", ret);
0473         goto err_connection_disable;
0474     }
0475 
0476     gb_pm_runtime_put_autosuspend(bundle);
0477 
0478     return 0;
0479 
0480 err_connection_disable:
0481     gb_connection_disable(connection);
0482 err_destroy_hid:
0483     hid_destroy_device(hid);
0484 err_connection_destroy:
0485     gb_connection_destroy(connection);
0486 err_free_ghid:
0487     kfree(ghid);
0488 
0489     return ret;
0490 }
0491 
0492 static void gb_hid_disconnect(struct gb_bundle *bundle)
0493 {
0494     struct gb_hid *ghid = greybus_get_drvdata(bundle);
0495 
0496     if (gb_pm_runtime_get_sync(bundle))
0497         gb_pm_runtime_get_noresume(bundle);
0498 
0499     hid_destroy_device(ghid->hid);
0500     gb_connection_disable(ghid->connection);
0501     gb_connection_destroy(ghid->connection);
0502     kfree(ghid);
0503 }
0504 
0505 static const struct greybus_bundle_id gb_hid_id_table[] = {
0506     { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
0507     { }
0508 };
0509 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
0510 
0511 static struct greybus_driver gb_hid_driver = {
0512     .name       = "hid",
0513     .probe      = gb_hid_probe,
0514     .disconnect = gb_hid_disconnect,
0515     .id_table   = gb_hid_id_table,
0516 };
0517 module_greybus_driver(gb_hid_driver);
0518 
0519 MODULE_LICENSE("GPL v2");