Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

Linux/drivers/hid/i2c-hid/i2c-hid.c

  1 /*
  2  * HID over I2C protocol implementation
  3  *
  4  * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  5  * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
  6  * Copyright (c) 2012 Red Hat, Inc
  7  *
  8  * This code is partly based on "USB HID support for Linux":
  9  *
 10  *  Copyright (c) 1999 Andreas Gal
 11  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
 12  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
 13  *  Copyright (c) 2007-2008 Oliver Neukum
 14  *  Copyright (c) 2006-2010 Jiri Kosina
 15  *
 16  * This file is subject to the terms and conditions of the GNU General Public
 17  * License.  See the file COPYING in the main directory of this archive for
 18  * more details.
 19  */
 20 
 21 #include <linux/module.h>
 22 #include <linux/i2c.h>
 23 #include <linux/interrupt.h>
 24 #include <linux/input.h>
 25 #include <linux/delay.h>
 26 #include <linux/slab.h>
 27 #include <linux/pm.h>
 28 #include <linux/pm_runtime.h>
 29 #include <linux/device.h>
 30 #include <linux/wait.h>
 31 #include <linux/err.h>
 32 #include <linux/string.h>
 33 #include <linux/list.h>
 34 #include <linux/jiffies.h>
 35 #include <linux/kernel.h>
 36 #include <linux/hid.h>
 37 #include <linux/mutex.h>
 38 #include <linux/acpi.h>
 39 #include <linux/of.h>
 40 
 41 #include <linux/i2c/i2c-hid.h>
 42 
 43 /* flags */
 44 #define I2C_HID_STARTED         (1 << 0)
 45 #define I2C_HID_RESET_PENDING   (1 << 1)
 46 #define I2C_HID_READ_PENDING    (1 << 2)
 47 
 48 #define I2C_HID_PWR_ON          0x00
 49 #define I2C_HID_PWR_SLEEP       0x01
 50 
 51 /* debug option */
 52 static bool debug;
 53 module_param(debug, bool, 0444);
 54 MODULE_PARM_DESC(debug, "print a lot of debug information");
 55 
 56 #define i2c_hid_dbg(ihid, fmt, arg...)                                    \
 57 do {                                                                      \
 58         if (debug)                                                        \
 59                 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
 60 } while (0)
 61 
 62 struct i2c_hid_desc {
 63         __le16 wHIDDescLength;
 64         __le16 bcdVersion;
 65         __le16 wReportDescLength;
 66         __le16 wReportDescRegister;
 67         __le16 wInputRegister;
 68         __le16 wMaxInputLength;
 69         __le16 wOutputRegister;
 70         __le16 wMaxOutputLength;
 71         __le16 wCommandRegister;
 72         __le16 wDataRegister;
 73         __le16 wVendorID;
 74         __le16 wProductID;
 75         __le16 wVersionID;
 76         __le32 reserved;
 77 } __packed;
 78 
 79 struct i2c_hid_cmd {
 80         unsigned int registerIndex;
 81         __u8 opcode;
 82         unsigned int length;
 83         bool wait;
 84 };
 85 
 86 union command {
 87         u8 data[0];
 88         struct cmd {
 89                 __le16 reg;
 90                 __u8 reportTypeID;
 91                 __u8 opcode;
 92         } __packed c;
 93 };
 94 
 95 #define I2C_HID_CMD(opcode_) \
 96         .opcode = opcode_, .length = 4, \
 97         .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
 98 
 99 /* fetch HID descriptor */
100 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
101 /* fetch report descriptors */
102 static const struct i2c_hid_cmd hid_report_descr_cmd = {
103                 .registerIndex = offsetof(struct i2c_hid_desc,
104                         wReportDescRegister),
105                 .opcode = 0x00,
106                 .length = 2 };
107 /* commands */
108 static const struct i2c_hid_cmd hid_reset_cmd =         { I2C_HID_CMD(0x01),
109                                                           .wait = true };
110 static const struct i2c_hid_cmd hid_get_report_cmd =    { I2C_HID_CMD(0x02) };
111 static const struct i2c_hid_cmd hid_set_report_cmd =    { I2C_HID_CMD(0x03) };
112 static const struct i2c_hid_cmd hid_set_power_cmd =     { I2C_HID_CMD(0x08) };
113 static const struct i2c_hid_cmd hid_no_cmd =            { .length = 0 };
114 
115 /*
116  * These definitions are not used here, but are defined by the spec.
117  * Keeping them here for documentation purposes.
118  *
119  * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
120  * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
121  * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
122  * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
123  */
124 
125 static DEFINE_MUTEX(i2c_hid_open_mut);
126 
127 /* The main device structure */
128 struct i2c_hid {
129         struct i2c_client       *client;        /* i2c client */
130         struct hid_device       *hid;   /* pointer to corresponding HID dev */
131         union {
132                 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
133                 struct i2c_hid_desc hdesc;      /* the HID Descriptor */
134         };
135         __le16                  wHIDDescRegister; /* location of the i2c
136                                                    * register of the HID
137                                                    * descriptor. */
138         unsigned int            bufsize;        /* i2c buffer size */
139         char                    *inbuf;         /* Input buffer */
140         char                    *rawbuf;        /* Raw Input buffer */
141         char                    *cmdbuf;        /* Command buffer */
142         char                    *argsbuf;       /* Command arguments buffer */
143 
144         unsigned long           flags;          /* device flags */
145 
146         wait_queue_head_t       wait;           /* For waiting the interrupt */
147 
148         struct i2c_hid_platform_data pdata;
149 };
150 
151 static int __i2c_hid_command(struct i2c_client *client,
152                 const struct i2c_hid_cmd *command, u8 reportID,
153                 u8 reportType, u8 *args, int args_len,
154                 unsigned char *buf_recv, int data_len)
155 {
156         struct i2c_hid *ihid = i2c_get_clientdata(client);
157         union command *cmd = (union command *)ihid->cmdbuf;
158         int ret;
159         struct i2c_msg msg[2];
160         int msg_num = 1;
161 
162         int length = command->length;
163         bool wait = command->wait;
164         unsigned int registerIndex = command->registerIndex;
165 
166         /* special case for hid_descr_cmd */
167         if (command == &hid_descr_cmd) {
168                 cmd->c.reg = ihid->wHIDDescRegister;
169         } else {
170                 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
171                 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
172         }
173 
174         if (length > 2) {
175                 cmd->c.opcode = command->opcode;
176                 cmd->c.reportTypeID = reportID | reportType << 4;
177         }
178 
179         memcpy(cmd->data + length, args, args_len);
180         length += args_len;
181 
182         i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
183 
184         msg[0].addr = client->addr;
185         msg[0].flags = client->flags & I2C_M_TEN;
186         msg[0].len = length;
187         msg[0].buf = cmd->data;
188         if (data_len > 0) {
189                 msg[1].addr = client->addr;
190                 msg[1].flags = client->flags & I2C_M_TEN;
191                 msg[1].flags |= I2C_M_RD;
192                 msg[1].len = data_len;
193                 msg[1].buf = buf_recv;
194                 msg_num = 2;
195                 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
196         }
197 
198         if (wait)
199                 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
200 
201         ret = i2c_transfer(client->adapter, msg, msg_num);
202 
203         if (data_len > 0)
204                 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
205 
206         if (ret != msg_num)
207                 return ret < 0 ? ret : -EIO;
208 
209         ret = 0;
210 
211         if (wait) {
212                 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
213                 if (!wait_event_timeout(ihid->wait,
214                                 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
215                                 msecs_to_jiffies(5000)))
216                         ret = -ENODATA;
217                 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
218         }
219 
220         return ret;
221 }
222 
223 static int i2c_hid_command(struct i2c_client *client,
224                 const struct i2c_hid_cmd *command,
225                 unsigned char *buf_recv, int data_len)
226 {
227         return __i2c_hid_command(client, command, 0, 0, NULL, 0,
228                                 buf_recv, data_len);
229 }
230 
231 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
232                 u8 reportID, unsigned char *buf_recv, int data_len)
233 {
234         struct i2c_hid *ihid = i2c_get_clientdata(client);
235         u8 args[3];
236         int ret;
237         int args_len = 0;
238         u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
239 
240         i2c_hid_dbg(ihid, "%s\n", __func__);
241 
242         if (reportID >= 0x0F) {
243                 args[args_len++] = reportID;
244                 reportID = 0x0F;
245         }
246 
247         args[args_len++] = readRegister & 0xFF;
248         args[args_len++] = readRegister >> 8;
249 
250         ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
251                 reportType, args, args_len, buf_recv, data_len);
252         if (ret) {
253                 dev_err(&client->dev,
254                         "failed to retrieve report from device.\n");
255                 return ret;
256         }
257 
258         return 0;
259 }
260 
261 /**
262  * i2c_hid_set_or_send_report: forward an incoming report to the device
263  * @client: the i2c_client of the device
264  * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
265  * @reportID: the report ID
266  * @buf: the actual data to transfer, without the report ID
267  * @len: size of buf
268  * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
269  */
270 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
271                 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
272 {
273         struct i2c_hid *ihid = i2c_get_clientdata(client);
274         u8 *args = ihid->argsbuf;
275         const struct i2c_hid_cmd *hidcmd;
276         int ret;
277         u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
278         u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
279         u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
280 
281         /* hid_hw_* already checked that data_len < HID_MAX_BUFFER_SIZE */
282         u16 size =      2                       /* size */ +
283                         (reportID ? 1 : 0)      /* reportID */ +
284                         data_len                /* buf */;
285         int args_len =  (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
286                         2                       /* dataRegister */ +
287                         size                    /* args */;
288         int index = 0;
289 
290         i2c_hid_dbg(ihid, "%s\n", __func__);
291 
292         if (!use_data && maxOutputLength == 0)
293                 return -ENOSYS;
294 
295         if (reportID >= 0x0F) {
296                 args[index++] = reportID;
297                 reportID = 0x0F;
298         }
299 
300         /*
301          * use the data register for feature reports or if the device does not
302          * support the output register
303          */
304         if (use_data) {
305                 args[index++] = dataRegister & 0xFF;
306                 args[index++] = dataRegister >> 8;
307                 hidcmd = &hid_set_report_cmd;
308         } else {
309                 args[index++] = outputRegister & 0xFF;
310                 args[index++] = outputRegister >> 8;
311                 hidcmd = &hid_no_cmd;
312         }
313 
314         args[index++] = size & 0xFF;
315         args[index++] = size >> 8;
316 
317         if (reportID)
318                 args[index++] = reportID;
319 
320         memcpy(&args[index], buf, data_len);
321 
322         ret = __i2c_hid_command(client, hidcmd, reportID,
323                 reportType, args, args_len, NULL, 0);
324         if (ret) {
325                 dev_err(&client->dev, "failed to set a report to device.\n");
326                 return ret;
327         }
328 
329         return data_len;
330 }
331 
332 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
333 {
334         struct i2c_hid *ihid = i2c_get_clientdata(client);
335         int ret;
336 
337         i2c_hid_dbg(ihid, "%s\n", __func__);
338 
339         ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
340                 0, NULL, 0, NULL, 0);
341         if (ret)
342                 dev_err(&client->dev, "failed to change power setting.\n");
343 
344         return ret;
345 }
346 
347 static int i2c_hid_hwreset(struct i2c_client *client)
348 {
349         struct i2c_hid *ihid = i2c_get_clientdata(client);
350         int ret;
351 
352         i2c_hid_dbg(ihid, "%s\n", __func__);
353 
354         ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
355         if (ret)
356                 return ret;
357 
358         i2c_hid_dbg(ihid, "resetting...\n");
359 
360         ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
361         if (ret) {
362                 dev_err(&client->dev, "failed to reset device.\n");
363                 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
364                 return ret;
365         }
366 
367         return 0;
368 }
369 
370 static void i2c_hid_get_input(struct i2c_hid *ihid)
371 {
372         int ret, ret_size;
373         int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
374 
375         if (size > ihid->bufsize)
376                 size = ihid->bufsize;
377 
378         ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
379         if (ret != size) {
380                 if (ret < 0)
381                         return;
382 
383                 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
384                         __func__, ret, size);
385                 return;
386         }
387 
388         ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
389 
390         if (!ret_size) {
391                 /* host or device initiated RESET completed */
392                 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
393                         wake_up(&ihid->wait);
394                 return;
395         }
396 
397         if (ret_size > size) {
398                 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
399                         __func__, size, ret_size);
400                 return;
401         }
402 
403         i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
404 
405         if (test_bit(I2C_HID_STARTED, &ihid->flags))
406                 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
407                                 ret_size - 2, 1);
408 
409         return;
410 }
411 
412 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
413 {
414         struct i2c_hid *ihid = dev_id;
415 
416         if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
417                 return IRQ_HANDLED;
418 
419         i2c_hid_get_input(ihid);
420 
421         return IRQ_HANDLED;
422 }
423 
424 static int i2c_hid_get_report_length(struct hid_report *report)
425 {
426         return ((report->size - 1) >> 3) + 1 +
427                 report->device->report_enum[report->type].numbered + 2;
428 }
429 
430 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
431         size_t bufsize)
432 {
433         struct hid_device *hid = report->device;
434         struct i2c_client *client = hid->driver_data;
435         struct i2c_hid *ihid = i2c_get_clientdata(client);
436         unsigned int size, ret_size;
437 
438         size = i2c_hid_get_report_length(report);
439         if (i2c_hid_get_report(client,
440                         report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
441                         report->id, buffer, size))
442                 return;
443 
444         i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer);
445 
446         ret_size = buffer[0] | (buffer[1] << 8);
447 
448         if (ret_size != size) {
449                 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
450                         __func__, size, ret_size);
451                 return;
452         }
453 
454         /* hid->driver_lock is held as we are in probe function,
455          * we just need to setup the input fields, so using
456          * hid_report_raw_event is safe. */
457         hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
458 }
459 
460 /*
461  * Initialize all reports
462  */
463 static void i2c_hid_init_reports(struct hid_device *hid)
464 {
465         struct hid_report *report;
466         struct i2c_client *client = hid->driver_data;
467         struct i2c_hid *ihid = i2c_get_clientdata(client);
468         u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
469 
470         if (!inbuf) {
471                 dev_err(&client->dev, "can not retrieve initial reports\n");
472                 return;
473         }
474 
475         /*
476          * The device must be powered on while we fetch initial reports
477          * from it.
478          */
479         pm_runtime_get_sync(&client->dev);
480 
481         list_for_each_entry(report,
482                 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
483                 i2c_hid_init_report(report, inbuf, ihid->bufsize);
484 
485         pm_runtime_put(&client->dev);
486 
487         kfree(inbuf);
488 }
489 
490 /*
491  * Traverse the supplied list of reports and find the longest
492  */
493 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
494                 unsigned int *max)
495 {
496         struct hid_report *report;
497         unsigned int size;
498 
499         /* We should not rely on wMaxInputLength, as some devices may set it to
500          * a wrong length. */
501         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
502                 size = i2c_hid_get_report_length(report);
503                 if (*max < size)
504                         *max = size;
505         }
506 }
507 
508 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
509 {
510         kfree(ihid->inbuf);
511         kfree(ihid->rawbuf);
512         kfree(ihid->argsbuf);
513         kfree(ihid->cmdbuf);
514         ihid->inbuf = NULL;
515         ihid->rawbuf = NULL;
516         ihid->cmdbuf = NULL;
517         ihid->argsbuf = NULL;
518         ihid->bufsize = 0;
519 }
520 
521 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
522 {
523         /* the worst case is computed from the set_report command with a
524          * reportID > 15 and the maximum report length */
525         int args_len = sizeof(__u8) + /* optional ReportID byte */
526                        sizeof(__u16) + /* data register */
527                        sizeof(__u16) + /* size of the report */
528                        report_size; /* report */
529 
530         ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
531         ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
532         ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
533         ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
534 
535         if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
536                 i2c_hid_free_buffers(ihid);
537                 return -ENOMEM;
538         }
539 
540         ihid->bufsize = report_size;
541 
542         return 0;
543 }
544 
545 static int i2c_hid_get_raw_report(struct hid_device *hid,
546                 unsigned char report_number, __u8 *buf, size_t count,
547                 unsigned char report_type)
548 {
549         struct i2c_client *client = hid->driver_data;
550         struct i2c_hid *ihid = i2c_get_clientdata(client);
551         size_t ret_count, ask_count;
552         int ret;
553 
554         if (report_type == HID_OUTPUT_REPORT)
555                 return -EINVAL;
556 
557         /* +2 bytes to include the size of the reply in the query buffer */
558         ask_count = min(count + 2, (size_t)ihid->bufsize);
559 
560         ret = i2c_hid_get_report(client,
561                         report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
562                         report_number, ihid->rawbuf, ask_count);
563 
564         if (ret < 0)
565                 return ret;
566 
567         ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
568 
569         if (ret_count <= 2)
570                 return 0;
571 
572         ret_count = min(ret_count, ask_count);
573 
574         /* The query buffer contains the size, dropping it in the reply */
575         count = min(count, ret_count - 2);
576         memcpy(buf, ihid->rawbuf + 2, count);
577 
578         return count;
579 }
580 
581 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
582                 size_t count, unsigned char report_type, bool use_data)
583 {
584         struct i2c_client *client = hid->driver_data;
585         int report_id = buf[0];
586         int ret;
587 
588         if (report_type == HID_INPUT_REPORT)
589                 return -EINVAL;
590 
591         if (report_id) {
592                 buf++;
593                 count--;
594         }
595 
596         ret = i2c_hid_set_or_send_report(client,
597                                 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
598                                 report_id, buf, count, use_data);
599 
600         if (report_id && ret >= 0)
601                 ret++; /* add report_id to the number of transfered bytes */
602 
603         return ret;
604 }
605 
606 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
607                 size_t count)
608 {
609         return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
610                         false);
611 }
612 
613 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
614                                __u8 *buf, size_t len, unsigned char rtype,
615                                int reqtype)
616 {
617         switch (reqtype) {
618         case HID_REQ_GET_REPORT:
619                 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
620         case HID_REQ_SET_REPORT:
621                 if (buf[0] != reportnum)
622                         return -EINVAL;
623                 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
624         default:
625                 return -EIO;
626         }
627 }
628 
629 static int i2c_hid_parse(struct hid_device *hid)
630 {
631         struct i2c_client *client = hid->driver_data;
632         struct i2c_hid *ihid = i2c_get_clientdata(client);
633         struct i2c_hid_desc *hdesc = &ihid->hdesc;
634         unsigned int rsize;
635         char *rdesc;
636         int ret;
637         int tries = 3;
638 
639         i2c_hid_dbg(ihid, "entering %s\n", __func__);
640 
641         rsize = le16_to_cpu(hdesc->wReportDescLength);
642         if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
643                 dbg_hid("weird size of report descriptor (%u)\n", rsize);
644                 return -EINVAL;
645         }
646 
647         do {
648                 ret = i2c_hid_hwreset(client);
649                 if (ret)
650                         msleep(1000);
651         } while (tries-- > 0 && ret);
652 
653         if (ret)
654                 return ret;
655 
656         rdesc = kzalloc(rsize, GFP_KERNEL);
657 
658         if (!rdesc) {
659                 dbg_hid("couldn't allocate rdesc memory\n");
660                 return -ENOMEM;
661         }
662 
663         i2c_hid_dbg(ihid, "asking HID report descriptor\n");
664 
665         ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
666         if (ret) {
667                 hid_err(hid, "reading report descriptor failed\n");
668                 kfree(rdesc);
669                 return -EIO;
670         }
671 
672         i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
673 
674         ret = hid_parse_report(hid, rdesc, rsize);
675         kfree(rdesc);
676         if (ret) {
677                 dbg_hid("parsing report descriptor failed\n");
678                 return ret;
679         }
680 
681         return 0;
682 }
683 
684 static int i2c_hid_start(struct hid_device *hid)
685 {
686         struct i2c_client *client = hid->driver_data;
687         struct i2c_hid *ihid = i2c_get_clientdata(client);
688         int ret;
689         unsigned int bufsize = HID_MIN_BUFFER_SIZE;
690 
691         i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
692         i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
693         i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
694 
695         if (bufsize > ihid->bufsize) {
696                 i2c_hid_free_buffers(ihid);
697 
698                 ret = i2c_hid_alloc_buffers(ihid, bufsize);
699 
700                 if (ret)
701                         return ret;
702         }
703 
704         if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
705                 i2c_hid_init_reports(hid);
706 
707         return 0;
708 }
709 
710 static void i2c_hid_stop(struct hid_device *hid)
711 {
712         hid->claimed = 0;
713 }
714 
715 static int i2c_hid_open(struct hid_device *hid)
716 {
717         struct i2c_client *client = hid->driver_data;
718         struct i2c_hid *ihid = i2c_get_clientdata(client);
719         int ret = 0;
720 
721         mutex_lock(&i2c_hid_open_mut);
722         if (!hid->open++) {
723                 ret = pm_runtime_get_sync(&client->dev);
724                 if (ret < 0) {
725                         hid->open--;
726                         goto done;
727                 }
728                 set_bit(I2C_HID_STARTED, &ihid->flags);
729         }
730 done:
731         mutex_unlock(&i2c_hid_open_mut);
732         return ret < 0 ? ret : 0;
733 }
734 
735 static void i2c_hid_close(struct hid_device *hid)
736 {
737         struct i2c_client *client = hid->driver_data;
738         struct i2c_hid *ihid = i2c_get_clientdata(client);
739 
740         /* protecting hid->open to make sure we don't restart
741          * data acquistion due to a resumption we no longer
742          * care about
743          */
744         mutex_lock(&i2c_hid_open_mut);
745         if (!--hid->open) {
746                 clear_bit(I2C_HID_STARTED, &ihid->flags);
747 
748                 /* Save some power */
749                 pm_runtime_put(&client->dev);
750         }
751         mutex_unlock(&i2c_hid_open_mut);
752 }
753 
754 static int i2c_hid_power(struct hid_device *hid, int lvl)
755 {
756         struct i2c_client *client = hid->driver_data;
757         struct i2c_hid *ihid = i2c_get_clientdata(client);
758 
759         i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
760 
761         switch (lvl) {
762         case PM_HINT_FULLON:
763                 pm_runtime_get_sync(&client->dev);
764                 break;
765         case PM_HINT_NORMAL:
766                 pm_runtime_put(&client->dev);
767                 break;
768         }
769         return 0;
770 }
771 
772 static struct hid_ll_driver i2c_hid_ll_driver = {
773         .parse = i2c_hid_parse,
774         .start = i2c_hid_start,
775         .stop = i2c_hid_stop,
776         .open = i2c_hid_open,
777         .close = i2c_hid_close,
778         .power = i2c_hid_power,
779         .output_report = i2c_hid_output_report,
780         .raw_request = i2c_hid_raw_request,
781 };
782 
783 static int i2c_hid_init_irq(struct i2c_client *client)
784 {
785         struct i2c_hid *ihid = i2c_get_clientdata(client);
786         int ret;
787 
788         dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
789 
790         ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
791                         IRQF_TRIGGER_LOW | IRQF_ONESHOT,
792                         client->name, ihid);
793         if (ret < 0) {
794                 dev_warn(&client->dev,
795                         "Could not register for %s interrupt, irq = %d,"
796                         " ret = %d\n",
797                         client->name, client->irq, ret);
798 
799                 return ret;
800         }
801 
802         return 0;
803 }
804 
805 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
806 {
807         struct i2c_client *client = ihid->client;
808         struct i2c_hid_desc *hdesc = &ihid->hdesc;
809         unsigned int dsize;
810         int ret;
811 
812         /* i2c hid fetch using a fixed descriptor size (30 bytes) */
813         i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
814         ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
815                                 sizeof(struct i2c_hid_desc));
816         if (ret) {
817                 dev_err(&client->dev, "hid_descr_cmd failed\n");
818                 return -ENODEV;
819         }
820 
821         /* Validate the length of HID descriptor, the 4 first bytes:
822          * bytes 0-1 -> length
823          * bytes 2-3 -> bcdVersion (has to be 1.00) */
824         /* check bcdVersion == 1.0 */
825         if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
826                 dev_err(&client->dev,
827                         "unexpected HID descriptor bcdVersion (0x%04hx)\n",
828                         le16_to_cpu(hdesc->bcdVersion));
829                 return -ENODEV;
830         }
831 
832         /* Descriptor length should be 30 bytes as per the specification */
833         dsize = le16_to_cpu(hdesc->wHIDDescLength);
834         if (dsize != sizeof(struct i2c_hid_desc)) {
835                 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
836                         dsize);
837                 return -ENODEV;
838         }
839         i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
840         return 0;
841 }
842 
843 #ifdef CONFIG_ACPI
844 static int i2c_hid_acpi_pdata(struct i2c_client *client,
845                 struct i2c_hid_platform_data *pdata)
846 {
847         static u8 i2c_hid_guid[] = {
848                 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
849                 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
850         };
851         union acpi_object *obj;
852         struct acpi_device *adev;
853         acpi_handle handle;
854 
855         handle = ACPI_HANDLE(&client->dev);
856         if (!handle || acpi_bus_get_device(handle, &adev))
857                 return -ENODEV;
858 
859         obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL,
860                                       ACPI_TYPE_INTEGER);
861         if (!obj) {
862                 dev_err(&client->dev, "device _DSM execution failed\n");
863                 return -ENODEV;
864         }
865 
866         pdata->hid_descriptor_address = obj->integer.value;
867         ACPI_FREE(obj);
868 
869         return 0;
870 }
871 
872 static const struct acpi_device_id i2c_hid_acpi_match[] = {
873         {"ACPI0C50", 0 },
874         {"PNP0C50", 0 },
875         { },
876 };
877 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
878 #else
879 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
880                 struct i2c_hid_platform_data *pdata)
881 {
882         return -ENODEV;
883 }
884 #endif
885 
886 #ifdef CONFIG_OF
887 static int i2c_hid_of_probe(struct i2c_client *client,
888                 struct i2c_hid_platform_data *pdata)
889 {
890         struct device *dev = &client->dev;
891         u32 val;
892         int ret;
893 
894         ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
895         if (ret) {
896                 dev_err(&client->dev, "HID register address not provided\n");
897                 return -ENODEV;
898         }
899         if (val >> 16) {
900                 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
901                         val);
902                 return -EINVAL;
903         }
904         pdata->hid_descriptor_address = val;
905 
906         return 0;
907 }
908 
909 static const struct of_device_id i2c_hid_of_match[] = {
910         { .compatible = "hid-over-i2c" },
911         {},
912 };
913 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
914 #else
915 static inline int i2c_hid_of_probe(struct i2c_client *client,
916                 struct i2c_hid_platform_data *pdata)
917 {
918         return -ENODEV;
919 }
920 #endif
921 
922 static int i2c_hid_probe(struct i2c_client *client,
923                          const struct i2c_device_id *dev_id)
924 {
925         int ret;
926         struct i2c_hid *ihid;
927         struct hid_device *hid;
928         __u16 hidRegister;
929         struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
930 
931         dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
932 
933         if (!client->irq) {
934                 dev_err(&client->dev,
935                         "HID over i2c has not been provided an Int IRQ\n");
936                 return -EINVAL;
937         }
938 
939         ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
940         if (!ihid)
941                 return -ENOMEM;
942 
943         if (client->dev.of_node) {
944                 ret = i2c_hid_of_probe(client, &ihid->pdata);
945                 if (ret)
946                         goto err;
947         } else if (!platform_data) {
948                 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
949                 if (ret) {
950                         dev_err(&client->dev,
951                                 "HID register address not provided\n");
952                         goto err;
953                 }
954         } else {
955                 ihid->pdata = *platform_data;
956         }
957 
958         i2c_set_clientdata(client, ihid);
959 
960         ihid->client = client;
961 
962         hidRegister = ihid->pdata.hid_descriptor_address;
963         ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
964 
965         init_waitqueue_head(&ihid->wait);
966 
967         /* we need to allocate the command buffer without knowing the maximum
968          * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
969          * real computation later. */
970         ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
971         if (ret < 0)
972                 goto err;
973 
974         pm_runtime_get_noresume(&client->dev);
975         pm_runtime_set_active(&client->dev);
976         pm_runtime_enable(&client->dev);
977 
978         ret = i2c_hid_fetch_hid_descriptor(ihid);
979         if (ret < 0)
980                 goto err_pm;
981 
982         ret = i2c_hid_init_irq(client);
983         if (ret < 0)
984                 goto err_pm;
985 
986         hid = hid_allocate_device();
987         if (IS_ERR(hid)) {
988                 ret = PTR_ERR(hid);
989                 goto err_irq;
990         }
991 
992         ihid->hid = hid;
993 
994         hid->driver_data = client;
995         hid->ll_driver = &i2c_hid_ll_driver;
996         hid->dev.parent = &client->dev;
997         ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
998         hid->bus = BUS_I2C;
999         hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1000         hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1001         hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1002 
1003         snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1004                  client->name, hid->vendor, hid->product);
1005 
1006         ret = hid_add_device(hid);
1007         if (ret) {
1008                 if (ret != -ENODEV)
1009                         hid_err(client, "can't add hid device: %d\n", ret);
1010                 goto err_mem_free;
1011         }
1012 
1013         pm_runtime_put(&client->dev);
1014         return 0;
1015 
1016 err_mem_free:
1017         hid_destroy_device(hid);
1018 
1019 err_irq:
1020         free_irq(client->irq, ihid);
1021 
1022 err_pm:
1023         pm_runtime_put_noidle(&client->dev);
1024         pm_runtime_disable(&client->dev);
1025 
1026 err:
1027         i2c_hid_free_buffers(ihid);
1028         kfree(ihid);
1029         return ret;
1030 }
1031 
1032 static int i2c_hid_remove(struct i2c_client *client)
1033 {
1034         struct i2c_hid *ihid = i2c_get_clientdata(client);
1035         struct hid_device *hid;
1036 
1037         pm_runtime_get_sync(&client->dev);
1038         pm_runtime_disable(&client->dev);
1039         pm_runtime_set_suspended(&client->dev);
1040         pm_runtime_put_noidle(&client->dev);
1041 
1042         hid = ihid->hid;
1043         hid_destroy_device(hid);
1044 
1045         free_irq(client->irq, ihid);
1046 
1047         if (ihid->bufsize)
1048                 i2c_hid_free_buffers(ihid);
1049 
1050         kfree(ihid);
1051 
1052         return 0;
1053 }
1054 
1055 #ifdef CONFIG_PM_SLEEP
1056 static int i2c_hid_suspend(struct device *dev)
1057 {
1058         struct i2c_client *client = to_i2c_client(dev);
1059         struct i2c_hid *ihid = i2c_get_clientdata(client);
1060         struct hid_device *hid = ihid->hid;
1061         int ret = 0;
1062 
1063         disable_irq(client->irq);
1064         if (device_may_wakeup(&client->dev))
1065                 enable_irq_wake(client->irq);
1066 
1067         if (hid->driver && hid->driver->suspend)
1068                 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1069 
1070         /* Save some power */
1071         i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1072 
1073         return ret;
1074 }
1075 
1076 static int i2c_hid_resume(struct device *dev)
1077 {
1078         int ret;
1079         struct i2c_client *client = to_i2c_client(dev);
1080         struct i2c_hid *ihid = i2c_get_clientdata(client);
1081         struct hid_device *hid = ihid->hid;
1082 
1083         enable_irq(client->irq);
1084         ret = i2c_hid_hwreset(client);
1085         if (ret)
1086                 return ret;
1087 
1088         if (device_may_wakeup(&client->dev))
1089                 disable_irq_wake(client->irq);
1090 
1091         if (hid->driver && hid->driver->reset_resume) {
1092                 ret = hid->driver->reset_resume(hid);
1093                 return ret;
1094         }
1095 
1096         return 0;
1097 }
1098 #endif
1099 
1100 #ifdef CONFIG_PM
1101 static int i2c_hid_runtime_suspend(struct device *dev)
1102 {
1103         struct i2c_client *client = to_i2c_client(dev);
1104 
1105         i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1106         disable_irq(client->irq);
1107         return 0;
1108 }
1109 
1110 static int i2c_hid_runtime_resume(struct device *dev)
1111 {
1112         struct i2c_client *client = to_i2c_client(dev);
1113 
1114         enable_irq(client->irq);
1115         i2c_hid_set_power(client, I2C_HID_PWR_ON);
1116         return 0;
1117 }
1118 #endif
1119 
1120 static const struct dev_pm_ops i2c_hid_pm = {
1121         SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1122         SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1123                            NULL)
1124 };
1125 
1126 static const struct i2c_device_id i2c_hid_id_table[] = {
1127         { "hid", 0 },
1128         { },
1129 };
1130 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1131 
1132 
1133 static struct i2c_driver i2c_hid_driver = {
1134         .driver = {
1135                 .name   = "i2c_hid",
1136                 .owner  = THIS_MODULE,
1137                 .pm     = &i2c_hid_pm,
1138                 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1139                 .of_match_table = of_match_ptr(i2c_hid_of_match),
1140         },
1141 
1142         .probe          = i2c_hid_probe,
1143         .remove         = i2c_hid_remove,
1144 
1145         .id_table       = i2c_hid_id_table,
1146 };
1147 
1148 module_i2c_driver(i2c_hid_driver);
1149 
1150 MODULE_DESCRIPTION("HID over I2C core driver");
1151 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1152 MODULE_LICENSE("GPL");
1153 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us