File: ddcci.c

package info (click to toggle)
ddcci-driver-linux 0.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 472 kB
  • sloc: ansic: 1,832; makefile: 123; sh: 3
file content (1929 lines) | stat: -rw-r--r-- 51,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
/*
 *  DDC/CI sub-bus driver
 *
 *  Copyright (c) 2015 Christoph Grenz
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/fcntl.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>

#include <linux/ddcci.h>

#define DDCCI_RECV_BUFFER_SIZE 130
#define DEVICE_NAME "ddcci"
#define DDCCI_MAX_CAP_CHUNKS 200

static unsigned int delay = 60;
static unsigned short autoprobe_addrs[127] = {0xF0, 0xF2, 0xF4, 0xF6, 0xF8};
static int autoprobe_addr_count = 5;

static bool module_initialized = false;
static dev_t ddcci_cdev_first;
static dev_t ddcci_cdev_next;
static dev_t ddcci_cdev_end;
static DEFINE_MUTEX(core_lock);

struct bus_type ddcci_bus_type;
EXPORT_SYMBOL_GPL(ddcci_bus_type);

/* Assert neccessary string array sizes  */
#ifndef sizeof_field
# define sizeof_field(t,m) FIELD_SIZEOF(t,m)
#endif
#ifndef static_assert
#  define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
#  define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
#endif
static_assert(sizeof_field(struct ddcci_device, prot) > 8);
static_assert(sizeof_field(struct ddcci_device, type) > 8);
static_assert(sizeof_field(struct ddcci_device, model) > 8);
static_assert(sizeof_field(struct ddcci_device, vendor) > 8);
static_assert(sizeof_field(struct ddcci_device, module) > 8);

/* Internal per-i2c-client driver data */
struct ddcci_bus_drv_data {
	unsigned long quirks;
	struct i2c_client *i2c_dev;
	struct mutex lock;
	unsigned char recv_buffer[DDCCI_RECV_BUFFER_SIZE];
};

/* Replace non-alphanumeric characters in a string (used for modalias) */
static void ddcci_modalias_clean(char *string, size_t n, char replacement)
{
	int i;
	for (i = 0; i < n; ++i) {
		char c = string[i];
		if (c == 0) {
			return;
		} else if (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || c > 'z') {
			string[i] = replacement;
		}
	}
}

/* Write a message to the DDC/CI bus using i2c_smbus_write_byte() */
static int __ddcci_write_bytewise(struct i2c_client *client, unsigned char addr,
				  bool p_flag, const unsigned char * __restrict buf,
				  unsigned char len)
{
	int ret = 0;
	unsigned char outer_addr = (unsigned char)(client->addr << 1);
	unsigned xor = outer_addr; /* initial xor value */

	/* Consistency checks */
	if (len > 127)
		return -EINVAL;

	/* Special case: sender to 0x6E is always 0x51 */
	if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
		addr = DDCCI_HOST_ADDR_ODD;
	} else {
		/* When sending the odd address is used */
		addr = addr | 1;
	}

	/* first byte: sender address */
	xor ^= addr;
	ret = i2c_smbus_write_byte(client, addr);
	if (ret < 0)
		return ret;

	/* second byte: protocol flag and message size */
	xor ^= ((p_flag << 7) | len);
	ret = i2c_smbus_write_byte(client, (p_flag << 7)|len);
	if (ret < 0)
		return ret;

	/* send payload */
	while (len--) {
		xor ^= (*buf);
		ret = i2c_smbus_write_byte(client, (*buf));
		if (ret < 0)
			return ret;
		buf++;
	}

	/* send checksum */
	ret = i2c_smbus_write_byte(client, xor);
	return ret;
}

/* Write a message to the DDC/CI bus using i2c_master_send() */
static int __ddcci_write_block(struct i2c_client *client, unsigned char addr,
			       unsigned char *sendbuf, bool p_flag,
			       const unsigned char *data, unsigned char len)
{
	unsigned char outer_addr = (unsigned char)(client->addr << 1);
	unsigned xor = outer_addr;	/* initial xor value */
	unsigned char *ptr = sendbuf;

	/* Consistency checks */
	if (len > 127)
		return -EINVAL;

	/* Special case: sender to 0x6E is always 0x51 */
	if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
		addr = DDCCI_HOST_ADDR_ODD;
	} else {
		/* When sending the odd address is used */
		addr = addr | 1;
	}

	/* first byte: sender address */
	xor ^= addr;
	*(ptr++) = addr;
	/* second byte: protocol flag and message size */
	xor ^= ((p_flag << 7) | len);
	*(ptr++) = (p_flag << 7)|len;
	/* payload */
	while (len--) {
		xor ^= (*data);
		*(ptr++) = (*data);
		data++;
	}
	/* checksum */
	(*ptr) = xor;

	/* Send it */
	return i2c_master_send(client, sendbuf, ptr - sendbuf + 1);
}

/*
 * Write a message to the DDC/CI bus.
 *
 * You must hold the bus mutex when calling this function.
 */
static int ddcci_write(struct i2c_client *client, unsigned char addr,
		       bool p_flag, const unsigned char *data,
		       unsigned char len)
{
	struct ddcci_bus_drv_data *drv_data;
	unsigned char *sendbuf;
	int ret;

	drv_data = i2c_get_clientdata(client);


	pr_debug("sending to %d:%02x:%02x: %*ph\n", client->adapter->nr,
		 client->addr << 1, addr, len, data);
	if (drv_data->quirks & DDCCI_QUIRK_WRITE_BYTEWISE) {
		ret = __ddcci_write_bytewise(client, addr, p_flag, data, len);
	} else {
		sendbuf = drv_data->recv_buffer;
		ret = __ddcci_write_block(client, addr, sendbuf, p_flag, data, len);
	}

	return ret;
}

/*
 * Read a response from the DDC/CI bus with headers directly into a buffer.
 * Always check for DDCCI_QUIRK_SKIP_FIRST_BYTE when using this function.
 * The returned length contains the whole unmodified response.
 * If -EMSGSIZE is returned, the buffer contains the response up to `len`.
 * If any other negative error code is returned, the buffer content is
 * unspecified.
 */
static int __ddcci_read(struct i2c_client *client, unsigned char addr,
			bool p_flag, unsigned long quirks, unsigned char *buf,
			unsigned char len)
{
	int i, payload_len, packet_length, ret;
	unsigned char xor = DDCCI_HOST_ADDR_EVEN;

	/* Consistency checks */
	if (len < 3)
		return -EINVAL;

	/* Read frame */
	ret = i2c_master_recv(client, buf, len);
	if (ret < 0)
		goto out_err;
	packet_length = ret;

	/* Skip first byte if quirk active */
	if ((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE) && ret > 0 && len > 0) {
		ret--;
		len--;
		buf++;
	}

	/* If answer too short (= incomplete) break out */
	if (ret < 3) {
		ret = -EIO;
		goto out_err;
	}

	/* validate first byte */
	if (unlikely(buf[0] != addr)) {
		ret = (buf[0] == '\0') ? -EAGAIN : -EIO;
		goto out_err;
	}

	/* validate second byte (protocol flag) */
	if (unlikely((buf[1] & 0x80) != (p_flag << 7))) {
		if (!p_flag || !(quirks & DDCCI_QUIRK_NO_PFLAG)) {
			ret = -EIO;
			goto out_err;
		}
	}

	/* get and check payload length */
	payload_len = buf[1] & 0x7F;
	if (3+payload_len > packet_length)
		return -EBADMSG;
	if (3+payload_len > len)
		return -EMSGSIZE;

	/* calculate checksum */
	for (i = 0; i < 3+payload_len; i++)
		xor ^= buf[i];

	/* verify checksum */
	if (xor != 0) {
		dev_err(&client->dev, "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n",
			xor, payload_len);
		ret = -EBADMSG;
		goto out_err;
	}

	/* return result */
	ret = payload_len+3+((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)?1:0);

out_err:
	return ret;
}

/*
 * Read a response from the DDC/CI bus
 *
 * You must hold the bus mutex when calling this function.
 */
static int ddcci_read(struct i2c_client *client, unsigned char addr,
		      bool p_flag, unsigned char *buf, unsigned char len)
{
	struct ddcci_bus_drv_data *drv_data;
	unsigned char *recvbuf;
	int ret;

	drv_data = i2c_get_clientdata(client);
	recvbuf = drv_data->recv_buffer;

	/* Read frame */
	ret = __ddcci_read(client, addr, p_flag,
		drv_data->quirks, recvbuf, DDCCI_RECV_BUFFER_SIZE);
	if (ret < 0)
		return ret;

	if (drv_data->quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)
		recvbuf++;

	/* return result */
	if (buf) {
		if (ret > 3) {
			ret = ret-3;
			/* copy to caller buffer */
			memcpy(buf, &recvbuf[2], (ret < len) ? ret : len);

			if (ret > len) {
				/* if message was truncated, return -EMSGSIZE */
				pr_debug("received from %d:%02x:%02x: [%u/%u] %*ph ...\n",
					 client->adapter->nr, client->addr << 1,
					 addr, ret, len, len, buf);
				ret = -EMSGSIZE;
			} else {
				pr_debug("received from %d:%02x:%02x: [%u/%u] %*ph\n",
					 client->adapter->nr, client->addr << 1,
					 addr, ret, len, ret, buf);
			}
		}
	}
	if (!(drv_data->quirks & DDCCI_QUIRK_WRITE_BYTEWISE)) {
		/* second read to clear buffers, needed on some devices */
		__ddcci_read(client, addr, true, drv_data->quirks, recvbuf, 1);
	}
	return ret;
}

/* Request the capability string for a device and put it into buf */
static int ddcci_get_caps(struct i2c_client *client, unsigned char addr,
			  unsigned char *buf, unsigned int len)
{
	int result = 0, counter = 0, offset = 0;
	unsigned char cmd[3] = { DDCCI_COMMAND_CAPS, 0x00, 0x00 };
	unsigned char *chunkbuf = kzalloc(35, GFP_KERNEL);

	if (!chunkbuf)
		return -ENOMEM;

	do {
		/* Send command */
		result = ddcci_write(client, addr, true, cmd, sizeof(cmd));
		if (result < 0)
			goto err_free;
		msleep(delay);
		/* read result chunk */
		result = ddcci_read(client, addr, true, chunkbuf,
				    (len > 32) ? 35 : len+3);
		if (result < 0)
			goto err_free;

		if (result > 0) {
			/* check chunk header */
			if (chunkbuf[0] != DDCCI_REPLY_CAPS) {
				result = -EIO;
				goto err_free;
			}
			if (chunkbuf[1] != cmd[1] || chunkbuf[2] != cmd[2]) {
				result = -EIO;
				goto err_free;
			}
			if (result < 3) {
				result = -EIO;
				goto err_free;
			}
			memcpy(buf, chunkbuf+3, min((unsigned int)result-3, len));

			counter++;
			/* adjust offset, etc. */
			offset += result-3;
			len -= result-3;
			buf += result-3;
			cmd[1] = offset >> 8;
			cmd[2] = offset & 0xFF;
			/* Another superfluous read to make some devices happy... */
			ddcci_read(client, addr, true, NULL, 2);
		}
	} while (result > 3 && counter < DDCCI_MAX_CAP_CHUNKS);

	kfree(chunkbuf);
	return offset+result-3;
err_free:
	kfree(chunkbuf);
	return result;
}

/*
 * Request the device identification and put it into buf.
 *
 * Also detects all communication quirks and sets the corresponding flags
 * in the ddcci_bus_drv_data structure associated with client.
 *
 * The identification command will fail on most DDC devices, as it is optional
 * to support, but even the "failed" response suffices to detect quirks.
 */
static int ddcci_identify_device(struct i2c_client *client, unsigned char addr,
				 unsigned char *buf, unsigned char len)
{
	int i, payload_len, ret = -ENODEV;
	unsigned long quirks;
	unsigned char cmd[1] = { DDCCI_COMMAND_ID };
	unsigned char *buffer;
	unsigned char xor = DDCCI_HOST_ADDR_EVEN;
	struct ddcci_bus_drv_data *bus_drv_data;

	bus_drv_data = i2c_get_clientdata(client);
	quirks = bus_drv_data->quirks;
	buffer = bus_drv_data->recv_buffer;

	/* Send Identification command */
	if (!(quirks & DDCCI_QUIRK_WRITE_BYTEWISE)) {
		ret = __ddcci_write_block(client, addr, buffer, true, cmd, sizeof(cmd));
		dev_dbg(&client->dev,
			"[%02x:%02x] writing identification command in block mode: %d\n",
			client->addr << 1, addr, ret);
		if ((ret == -ENXIO)
		    && i2c_check_functionality(client->adapter,
					       I2C_FUNC_SMBUS_WRITE_BYTE)) {
			quirks |= DDCCI_QUIRK_WRITE_BYTEWISE;
			dev_info(&client->dev,
				"DDC/CI bus quirk detected: writes must be done bytewise\n");
			/* Some devices need writing twice after a failed blockwise write */
			__ddcci_write_bytewise(client, addr, true, cmd, sizeof(cmd));
			msleep(delay);
		}
	}
	if (ret < 0 && (quirks & DDCCI_QUIRK_WRITE_BYTEWISE)) {
		ret = __ddcci_write_bytewise(client, addr, true, cmd, sizeof(cmd));
		dev_dbg(&client->dev,
			"[%02x:%02x] writing identification command in bytewise mode: %d\n",
			client->addr << 1, addr, ret);
	}
	if (ret < 0)
		return -ENODEV;

	/* Wait */
	msleep(delay);

	/* Receive response */
	ret = i2c_master_recv(client, buffer, DDCCI_RECV_BUFFER_SIZE);
	if (ret < 0) {
		dev_dbg(&client->dev,
			"[%02x:%02x] receiving identification response resulted in errno %d\n",
			client->addr << 1, addr, ret);
		return ret;
	}

	if (ret == 0) {
		dev_dbg(&client->dev,
			"[%02x:%02x] no identification response received\n",
			client->addr << 1, addr);
		return ret;
	}

	/* Skip first byte if quirk already active */
	if (quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE && ret > 1) {
		dev_dbg(&client->dev,
			"[%02x:%02x] doubled first byte quirk in effect\n",
			client->addr << 1, addr);
		ret--;
		buffer++;
	}

	/* If answer too short (= incomplete) break out */
	if (ret < 3) {
		dev_dbg(&client->dev,
			"[%02x:%02x] identification response is too short (%d bytes)\n",
			client->addr << 1, addr, ret);
		return -EIO;
	}

	/* validate first byte */
	if (buffer[0] != addr) {
		dev_dbg(&client->dev,
			"[%02x:%02x] identification response: %*ph\n",
			client->addr << 1, addr, (ret > 32 ? 32 : ret), buffer);

		dev_dbg(&client->dev,
			"[%02x:%02x] identification response invalid (expected first byte %02x, got %02x)\n",
			client->addr << 1, addr, addr, buffer[0]);
		return -ENODEV;
	}

	/* Check if first byte is doubled (QUIRK_SKIP_FIRST_BYTE) */
	if (!(quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)) {
		if (buffer[0] == buffer[1]) {
			quirks |= DDCCI_QUIRK_SKIP_FIRST_BYTE;
			dev_info(&client->dev,
				"DDC/CI bus quirk detected: doubled first byte on read\n");
			ret--;
			buffer++;
			if (ret < 3)
				return -EIO;
		}
	}

	/* validate second byte (protocol flag) */
	if ((buffer[1] & 0x80) != 0x80 && !(quirks & DDCCI_QUIRK_NO_PFLAG)) {
		dev_info(&client->dev,
			"DDC/CI bus quirk detected: device omits protocol flag on responses\n");
		quirks |= DDCCI_QUIRK_NO_PFLAG;
	}

	/* get and check payload length */
	payload_len = buffer[1] & 0x7F;
	if (3+payload_len > ret) {
		dev_dbg(&client->dev,
			"[%02x:%02x] identification response: %*ph ...\n",
			client->addr << 1, addr, ret, buffer);
		dev_dbg(&client->dev,
			"[%02x:%02x] identification response was truncated (expected %d bytes, got %d)\n",
			client->addr << 1, addr, 3+payload_len, ret);
		return -EBADMSG;
	}

	dev_dbg(&client->dev,
		"[%02x:%02x] identification response: %*ph\n",
		client->addr << 1, addr, 3+payload_len, buffer);

	/* calculate checksum */
	for (i = 0; i < 3+payload_len; i++)
		xor ^= buffer[i];

	/* verify checksum */
	if (xor != 0) {
		dev_err(&client->dev,
			"[%02x:%02x] invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n",
			client->addr << 1, addr, xor, payload_len);
		return -EBADMSG;
	}

	/* save quirks */
	bus_drv_data->quirks = quirks;

	/* return result */
	if (payload_len <= len) {
		ret = payload_len;
		memcpy(buf, &buffer[2], payload_len);
	} else {
		ret = -EMSGSIZE;
		memcpy(buf, &buffer[2], len);
	}
	return ret;
}

/* Character device */

/* Data structure for an open file handle */
struct ddcci_fp_data {
	struct ddcci_device *dev;
	bool exclusive;
	unsigned char buffer[129];
};

/* Called when the character device is opened */
static int ddcci_cdev_open(struct inode *inode, struct file *filp)
{
	struct ddcci_device *dev = container_of(inode->i_cdev,
						struct ddcci_device, cdev);
	struct ddcci_fp_data *fp_data = NULL;

	fp_data = kzalloc(sizeof(struct ddcci_fp_data), GFP_KERNEL);

	if (!fp_data)
		return -ENOMEM;

	fp_data->exclusive = filp->f_flags & O_EXCL;

	if (fp_data->exclusive) {
		if (down_write_trylock(&dev->cdev_sem) == 0) {
			kfree(fp_data);
			return -EBUSY;
		}
	} else {
		if (down_read_trylock(&dev->cdev_sem) == 0) {
			kfree(fp_data);
			return -EBUSY;
		}
	}

	fp_data->dev = dev;
	filp->private_data = fp_data;

	return 0;
}

/* Called when the character device is closed */
static int ddcci_cdev_close(struct inode *inode, struct file *filp)
{
	struct ddcci_fp_data *fp_data = filp->private_data;
	struct ddcci_device *dev = fp_data->dev;

	if (fp_data->exclusive)
		up_write(&dev->cdev_sem);
	else
		up_read(&dev->cdev_sem);

	filp->private_data = NULL;
	kfree(fp_data);
	return 0;
}

/* Called when reading from the character device */
static ssize_t ddcci_cdev_read(struct file *filp, char __user *buffer,
			       size_t length, loff_t *offset)
{
	struct ddcci_fp_data *fp_data = filp->private_data;
	struct ddcci_device *dev = fp_data->dev;
	unsigned char *buf = fp_data->buffer;
	const bool nonblocking = (filp->f_flags & O_NONBLOCK) != 0;
	int ret = 0;

	if ((filp->f_mode & FMODE_READ) == 0)
		return -EBADF;

	/* Lock mutex */
	if (nonblocking) {
		if (mutex_trylock(&dev->bus_drv_data->lock))
			return -EAGAIN;
	} else {
		if (mutex_lock_interruptible(&dev->bus_drv_data->lock))
			return -ERESTARTSYS;
	}

	/* Execute read */
	ret = ddcci_read(dev->bus_drv_data->i2c_dev, dev->inner_addr, true, buf,
			 length);

	if (ret > 0) {
		/* Copy data from user space */
		if (copy_to_user(buffer, buf, ret)) {
			ret = -EFAULT;
			goto out;
		}
	}

out:
	mutex_unlock(&dev->bus_drv_data->lock);
	return ret;
}

/* Called when writing to the character device */
static ssize_t ddcci_cdev_write(struct file *filp, const char __user *buffer,
				size_t count, loff_t *offset)
{
	struct ddcci_fp_data *fp_data = filp->private_data;
	struct ddcci_device *dev = fp_data->dev;
	unsigned char *buf = fp_data->buffer;
	const bool nonblocking = (filp->f_flags & O_NONBLOCK) != 0;
	int ret = 0;

	if ((filp->f_mode & FMODE_WRITE) == 0)
		return -EBADF;

	if (count > 127)
		return -EINVAL;

	/* Lock mutex */
	if (nonblocking) {
		if (mutex_trylock(&dev->bus_drv_data->lock))
			return -EAGAIN;
	} else {
		if (mutex_lock_interruptible(&dev->bus_drv_data->lock))
			return -ERESTARTSYS;
	}

	if (count > 0) {
		/* Copy data from user space */
		if (copy_from_user(buf, buffer, count)) {
			ret = -EFAULT;
			goto err_out;
		}

		/* Execute write */
		ret = ddcci_write(dev->bus_drv_data->i2c_dev, dev->inner_addr,
				  true, buf, count);
	}

	if (ret >= 0) {
		msleep(delay);
		mutex_unlock(&dev->bus_drv_data->lock);
		return count;
	}

err_out:
	mutex_unlock(&dev->bus_drv_data->lock);
	return ret;
}

/* Called when seeking the character device */
static loff_t ddcci_cdev_seek(struct file *filp, loff_t offset, int anchor)
{
	return -EINVAL;
}

static const struct file_operations ddcci_fops = {
	.owner = THIS_MODULE,
	.read = ddcci_cdev_read,
	.write = ddcci_cdev_write,
	.open = ddcci_cdev_open,
	.release = ddcci_cdev_close,
	.llseek = ddcci_cdev_seek
};

/* Set up the character device for a DDC/CI device */
static int ddcci_setup_char_device(struct ddcci_device *device)
{
	int ret = -EINVAL;

	/* Check if free minor exists */
	if (ddcci_cdev_next == ddcci_cdev_end) {
		dev_err(&device->dev, "no free major/minor\n");
		ret = -ENFILE;
		goto out;
	}

	/* Initialize rwsem */
	init_rwsem(&device->cdev_sem);

	/* Initialize character device node */
	cdev_init(&device->cdev, &ddcci_fops);
	device->cdev.owner = THIS_MODULE;

	/* Publish char device */
	device->dev.devt = ddcci_cdev_next;
	ret = cdev_add(&device->cdev, ddcci_cdev_next, 1);
	if (ret) {
		device->dev.devt = 0;
		goto out;
	}

	ddcci_cdev_next++;
out:
	return ret;
}

/* sysfs attributes */

static ssize_t ddcci_attr_capabilities_show(struct device *dev,
					    struct device_attribute *attr,
					    char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	size_t len;

	if (likely(device != NULL)) {
		len = device->capabilities_len;
		if (unlikely(len > PAGE_SIZE))
			len = PAGE_SIZE;
		if (len == 0) {
			ret = len;
		} else {
			memcpy(buf, device->capabilities, len);
			if (likely(len < PAGE_SIZE)) {
				buf[len] = '\n';
				ret = len+1;
			}
		}
	}

	return ret;
}

static ssize_t ddcci_attr_prot_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	size_t len;

	if (likely(device != NULL)) {
		len = strnlen(device->prot, sizeof(device->prot));
		strncpy(buf, device->prot, PAGE_SIZE);
		if (len == 0) {
			ret = len;
		} else if (likely(len < PAGE_SIZE)) {
			buf[len] = '\n';
			ret = len+1;
		} else {
			ret = PAGE_SIZE;
		}
	}
	return ret;
}

static ssize_t ddcci_attr_type_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	size_t len;

	if (likely(device != NULL)) {
		len = strnlen(device->type, sizeof(device->type));
		strncpy(buf, device->type, PAGE_SIZE);
		if (len == 0) {
			ret = len;
		} else if (likely(len < PAGE_SIZE)) {
			buf[len] = '\n';
			ret = len+1;
		} else {
			ret = PAGE_SIZE;
		}
	}
	return ret;
}

static ssize_t ddcci_attr_model_show(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	size_t len;

	if (likely(device != NULL)) {
		len = strnlen(device->model, sizeof(device->model));
		strncpy(buf, device->model, PAGE_SIZE);
		if (len == 0) {
			ret = len;
		} else if (likely(len < PAGE_SIZE)) {
			buf[len] = '\n';
			ret = len+1;
		} else {
			ret = PAGE_SIZE;
		}
	}
	return ret;
}

static ssize_t ddcci_attr_vendor_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	size_t len;

	if (likely(device != NULL)) {
		len = strnlen(device->vendor, sizeof(device->vendor));
		strncpy(buf, device->vendor, PAGE_SIZE);
		if (len == 0) {
			ret = len;
		} else if (likely(len < PAGE_SIZE)) {
			buf[len] = '\n';
			ret = len+1;
		} else {
			ret = PAGE_SIZE;
		}
	}
	return ret;
}

static ssize_t ddcci_attr_module_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	size_t len;

	if (likely(device != NULL)) {
		len = strnlen(device->module, sizeof(device->module));
		strncpy(buf, device->module, PAGE_SIZE);
		if (len == 0) {
			ret = len;
		} else if (likely(len < PAGE_SIZE)) {
			buf[len] = '\n';
			ret = len+1;
		} else {
			ret = PAGE_SIZE;
		}
	}
	return ret;
}

static ssize_t ddcci_attr_serial_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;

	if (likely(device != NULL))
		ret = scnprintf(buf, PAGE_SIZE, "%d\n", device->device_number);

	return ret;
}

static ssize_t ddcci_attr_modalias_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
	struct ddcci_device *device = ddcci_verify_device(dev);
	ssize_t ret = -ENOENT;
	char model[ARRAY_SIZE(device->model)];
	char vendor[ARRAY_SIZE(device->model)];
	char module[ARRAY_SIZE(device->model)];

	if (likely(device != NULL)) {
		memcpy(model, device->model, sizeof(model));
		memcpy(vendor, device->vendor, sizeof(vendor));
		memcpy(module, device->module, sizeof(module));
		ddcci_modalias_clean(model, sizeof(model), '_');
		ddcci_modalias_clean(vendor, sizeof(vendor), '_');
		ddcci_modalias_clean(module, sizeof(module), '_');

		ret = scnprintf(buf, PAGE_SIZE, "%s%s-%s-%s-%s-%s\n",
			DDCCI_MODULE_PREFIX,
			device->prot,
			device->type,
			model,
			vendor,
			module
		);
	}
	return ret;
}

static DEVICE_ATTR(capabilities, S_IRUGO, ddcci_attr_capabilities_show, NULL);
static DEVICE_ATTR(idProt, S_IRUGO, ddcci_attr_prot_show, NULL);
static DEVICE_ATTR(idType, S_IRUGO, ddcci_attr_type_show, NULL);
static DEVICE_ATTR(idModel, S_IRUGO, ddcci_attr_model_show, NULL);
static DEVICE_ATTR(idVendor, S_IRUGO, ddcci_attr_vendor_show, NULL);
static DEVICE_ATTR(idModule, S_IRUGO, ddcci_attr_module_show, NULL);
static DEVICE_ATTR(idSerial, S_IRUGO, ddcci_attr_serial_show, NULL);
static DEVICE_ATTR(modalias, S_IRUGO, ddcci_attr_modalias_show, NULL);

static struct attribute *ddcci_char_device_attrs[] = {
	&dev_attr_capabilities.attr,
	&dev_attr_idProt.attr,
	&dev_attr_idType.attr,
	&dev_attr_idModel.attr,
	&dev_attr_idVendor.attr,
	&dev_attr_idModule.attr,
	&dev_attr_idSerial.attr,
	&dev_attr_modalias.attr,
	NULL,
};
ATTRIBUTE_GROUPS(ddcci_char_device);

/* DDC/CI bus */

static int ddcci_device_uevent(CSTRUCT device *dev, struct kobj_uevent_env *env)
{
	struct ddcci_device	*device = to_ddcci_device(dev);
	char model[ARRAY_SIZE(device->model)];
	char vendor[ARRAY_SIZE(device->vendor)];
	char module[ARRAY_SIZE(device->module)];

	memcpy(model, device->model, sizeof(model));
	memcpy(vendor, device->vendor, sizeof(vendor));
	memcpy(module, device->module, sizeof(module));
	ddcci_modalias_clean(model, sizeof(model), '_');
	ddcci_modalias_clean(vendor, sizeof(vendor), '_');
	ddcci_modalias_clean(module, sizeof(module), '_');

	if (add_uevent_var(env, "MODALIAS=%s%s-%s-%s-%s-%s",
			   DDCCI_MODULE_PREFIX,
			   device->prot,
			   device->type,
			   model,
			   vendor,
			   module
		))
		return -ENOMEM;

	if (device->prot[0])
		if (add_uevent_var(env, "DDCCI_PROT=%s", device->prot))
			return -ENOMEM;

	if (device->type[0])
		if (add_uevent_var(env, "DDCCI_TYPE=%s", device->type))
			return -ENOMEM;

	if (device->model[0])
		if (add_uevent_var(env, "DDCCI_MODEL=%s", device->model))
			return -ENOMEM;

	if (device->vendor[0]) {
		if (add_uevent_var(env, "DDCCI_VENDOR=%s", device->vendor))
			return -ENOMEM;

		if (add_uevent_var(env, "DDCCI_MODULE=%s", device->module))
			return -ENOMEM;

		if (add_uevent_var(env, "DDCCI_UNIQ=%d", device->device_number))
			return -ENOMEM;
	}

	return 0;
}

static void ddcci_device_release(struct device *dev)
{
	struct ddcci_device *device = to_ddcci_device(dev);
	struct ddcci_driver *driver;

	/* Notify driver */
	if (dev->driver) {
		driver = to_ddcci_driver(dev->driver);
		if (driver->remove)
			driver->remove(device);
	}

	/* Teardown chardev */
	if (dev->devt) {
		mutex_lock(&core_lock);
		if (device->cdev.dev == ddcci_cdev_next-1)
			ddcci_cdev_next--;
		cdev_del(&device->cdev);
		mutex_unlock(&core_lock);
	}

	/* Free capability string */
	if (device->capabilities) {
		device->capabilities_len = 0;
		kfree(device->capabilities);
	}
	/* Free device */
	kfree(device);
}

static char *ddcci_devnode(CSTRUCT device *dev,
			 umode_t *mode, kuid_t *uid, kgid_t *gid)
{
	struct ddcci_device *device;

	device = to_ddcci_device(dev);
	return kasprintf(GFP_KERNEL, "bus/ddcci/%d/display",
			 device->i2c_client->adapter->nr);
}

static char *ddcci_dependent_devnode(CSTRUCT device *dev,
			 umode_t *mode, kuid_t *uid, kgid_t *gid)
{
	struct ddcci_device *device;

	device = to_ddcci_device(dev);
	if (device->flags & DDCCI_FLAG_EXTERNAL) {
		if (device->outer_addr == device->inner_addr)
			return kasprintf(GFP_KERNEL, "bus/ddcci/%d/e%02x",
					 device->i2c_client->adapter->nr,
					 device->outer_addr);
		else
			return kasprintf(GFP_KERNEL, "bus/ddcci/%d/e%02x%02x",
					 device->i2c_client->adapter->nr,
					 device->outer_addr, device->inner_addr);
	} else {
		return kasprintf(GFP_KERNEL, "bus/ddcci/%d/i%02x",
				 device->i2c_client->adapter->nr,
				 device->inner_addr);
	}
}

/* Device type for main DDC/CI devices*/
static struct device_type ddcci_device_type = {
	.name	= "ddcci-device",
	.uevent		= ddcci_device_uevent,
	.groups		= ddcci_char_device_groups,
	.release	= ddcci_device_release,
	.devnode	= ddcci_devnode
};

/* Device type for dependent DDC/CI devices*/
static struct device_type ddcci_dependent_type = {
	.name	= "ddcci-dependent-device",
	.uevent		= ddcci_device_uevent,
	.groups		= ddcci_char_device_groups,
	.release	= ddcci_device_release,
	.devnode	= ddcci_dependent_devnode
};

/**
 * ddcci_verify_device - return parameter as ddcci_device, or NULL
 * @dev: device, probably from some driver model iterator
 */
struct ddcci_device *ddcci_verify_device(CSTRUCT device *dev)
{
	if (unlikely(!dev))
		return NULL;
	return (dev->type == &ddcci_device_type
		|| dev->type == &ddcci_dependent_type)
			? to_ddcci_device(dev)
			: NULL;
}
EXPORT_SYMBOL(ddcci_verify_device);

/**
 * ddcci_quirks - Get quirks for DDC/CI device
 * @dev: Target DDC/CI device
 */
unsigned long ddcci_quirks(struct ddcci_device *dev)
{
	if (unlikely(WARN_ON(!dev)))
		return ~0L;
	if (unlikely(WARN_ON(!dev->bus_drv_data)))
		return ~0L;
	return dev->bus_drv_data->quirks;
}
EXPORT_SYMBOL(ddcci_quirks);

/**
 * ddcci_register_driver - register DDC/CI driver
 * @owner: the owning module
 * @driver: the driver to register
 */
int ddcci_register_driver(struct module *owner, struct ddcci_driver *driver)
{
	int ret;

	/* Can't register until after driver model init */
	if (unlikely(WARN_ON(!smp_load_acquire(&module_initialized))))
		return -EAGAIN;

	pr_debug("registering driver [%s]\n", driver->driver.name);

	/* add the driver to the list of ddcci drivers in the driver core */
	driver->driver.owner = owner;
	driver->driver.bus = &ddcci_bus_type;

	/* When registration returns, the driver core
	 * will have called probe() for all matching-but-unbound devices.
	 */
	ret = driver_register(&driver->driver);
	if (ret)
		return ret;

	pr_debug("driver [%s] registered\n", driver->driver.name);

	return 0;
}
EXPORT_SYMBOL(ddcci_register_driver);

/**
 * ddcci_del_driver - unregister DDC/CI driver
 * @driver: the driver being unregistered
 */
void ddcci_del_driver(struct ddcci_driver *driver)
{
	driver_unregister(&driver->driver);
	pr_debug("driver [%s] unregistered\n", driver->driver.name);
}
EXPORT_SYMBOL(ddcci_del_driver);

/**
 * ddcci_device_write - Write a message to a DDC/CI device
 * @dev: Target DDC/CI device
 * @p_flag: Protocol flag, true for standard control messages
 * @data: Data that will be written to the device
 * @length: How many bytes to write
 *
 * Writes the message to the device and sleeps (see module parameter 'delay')
 */
int ddcci_device_write(struct ddcci_device *dev, bool p_flag,
		       unsigned char *data, unsigned char length)
{
	int ret;

	if (mutex_lock_interruptible(&dev->bus_drv_data->lock))
		return -EAGAIN;

	ret = ddcci_write(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, data, length);
	msleep(delay);
	mutex_unlock(&dev->bus_drv_data->lock);
	return ret;
}
EXPORT_SYMBOL(ddcci_device_write);

/**
 * ddcci_device_read - Read a response from a DDC/CI device
 * @dev: Target DDC/CI device
 * @p_flag: Protocol flag, must match the corresponding write
 * @buffer: Where to store data read from the device
 * @length: Buffer size
 */
int ddcci_device_read(struct ddcci_device *dev, bool p_flag,
		      unsigned char *buffer, unsigned char length)
{
	int ret;

	if (mutex_lock_interruptible(&dev->bus_drv_data->lock))
		return -EAGAIN;

	ret = ddcci_read(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, buffer, length);
	mutex_unlock(&dev->bus_drv_data->lock);
	return ret;
}
EXPORT_SYMBOL(ddcci_device_read);

/**
 * ddcci_device_writeread - Write a message to a device and read the response
 * @dev: Target DDC/CI device
 * @p_flag: Protocol flag, true for standard control messages
 * @buffer: Buffer used for write and read
 * @length: How many bytes to write
 * @maxlength: Buffer size on read
 *
 * Writing, sleeping and reading are done without releasing the DDC/CI bus.
 * This provides atomicity in respect to other DDC/CI accesses on the same bus.
 */
int ddcci_device_writeread(struct ddcci_device *dev, bool p_flag,
			   unsigned char *buffer, unsigned char length,
			   unsigned char maxlength)
{
	int ret;

	if (mutex_lock_interruptible(&dev->bus_drv_data->lock))
		return -EAGAIN;

	ret = ddcci_write(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, buffer, length);
	if (ret < 0)
		goto err;
	msleep(delay);
	ret = ddcci_read(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, buffer, maxlength);
err:
	mutex_unlock(&dev->bus_drv_data->lock);
	return ret;
}
EXPORT_SYMBOL(ddcci_device_writeread);

#define IS_ANY_ID(x) (((x)[0] == '\xFF') && ((x)[7] == '\xFF'))

/* Check if any device id in the array matches the device and return the matching id */
static const struct ddcci_device_id *ddcci_match_id(const struct ddcci_device_id *id,
						    const struct ddcci_device *device)
{
	while (id->prot[0] || id->type[0] || id->model[0] || id->vendor[0] || id->module[0]) {
		if ((IS_ANY_ID(id->prot) || (strcmp(device->prot, id->prot) == 0))
		 && (IS_ANY_ID(id->type) || (strcmp(device->type, id->type) == 0))
		 && (IS_ANY_ID(id->model) || (strcmp(device->model, id->model) == 0))
		 && (IS_ANY_ID(id->vendor) || (strcmp(device->vendor, id->vendor) == 0))
		 && (IS_ANY_ID(id->module) || (strcmp(device->module, id->module) == 0))) {
			return id;
		}
		id++;
	}
	return NULL;
}

static int ddcci_device_match(struct device *dev, DDCCI_DRV_CONST struct device_driver *drv)
{
	struct ddcci_device	*device = ddcci_verify_device(dev);
	DDCCI_DRV_CONST struct ddcci_driver	*driver;

	if (!device)
		return 0;

	driver = to_ddcci_driver(drv);
	/* match on an id table if there is one */
	if (driver->id_table)
		return ddcci_match_id(driver->id_table, device) != NULL;

	return 0;
}

static int ddcci_device_probe(struct device *dev)
{
	struct ddcci_device	*device = ddcci_verify_device(dev);
	DDCCI_DRV_CONST struct ddcci_driver	*driver;
	const struct ddcci_device_id *id;
	int ret = 0;

	if (!device)
		return -EINVAL;
	driver = to_ddcci_driver(dev->driver);

	id = ddcci_match_id(driver->id_table, device);
	if (!id)
		return -ENODEV;

	if (driver->probe)
		ret = driver->probe(device, id);

	return ret;
}

static int ddcci_device_remove(struct device *dev)
{
	struct ddcci_device	*device = ddcci_verify_device(dev);
	DDCCI_DRV_CONST struct ddcci_driver	*driver;
	int ret = 0;

	if (!device)
		return -EINVAL;
	driver = to_ddcci_driver(dev->driver);

	if (driver->remove)
		ret = driver->remove(device);

	return ret;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
static void ddcci_device_remove_void(struct device *dev)
{
	ddcci_device_remove(dev);
}
#endif

/**
 * DDCCI bus type structure
 */
struct bus_type ddcci_bus_type = {
	.name		= "ddcci",
	.match		= ddcci_device_match,
	.probe		= ddcci_device_probe,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
	.remove		= ddcci_device_remove_void
#else
	.remove		= ddcci_device_remove
#endif
};

/* Main I2C driver */

/* Get a pointer to the closing parenthesis */
static char *ddcci_capstr_tok(const char *s, int depth)
{
	const char *ptr = s;
	char *end;

	if (s == NULL || s[0] == '\0')
		return NULL;

	while ((end = strpbrk(ptr, "()"))) {
		if (!end || depth == INT_MAX)
			return NULL;
		if (*end == '(')
			depth++;
		else if (depth > 0)
			depth--;
		else
			break;
		ptr = end+1;
	}
	return end;
}

/**
 * ddcci_find_capstr_item - Search capability string for a tag
 * @capabilities: Capability string to search
 * @tag: Tag to find
 * @length: Buffer for the length of the found tag value (optional)
 *
 * Return a pointer to the start of the tag value (directly after the '(') on
 * success and write the length of the value (excluding the ')') into `length`.
 *
 * If the tag is not found or another error occurs, an ERR_PTR is returned
 * and `length` stays untouched.
 */
const char *ddcci_find_capstr_item(const char * capabilities,
				   const char * __restrict tag,
				   size_t *length)
{
	const char *src = capabilities, *ptr;
	ptrdiff_t len;
	int taglen = strnlen(tag, 1024);

	/* Check length of requested tag */
	if (unlikely(taglen <= 0 || taglen > 1023))
		return ERR_PTR(-EINVAL);

	/* Find tag */
	while (src && (strncmp(src+1, tag, taglen) != 0 || src[1+taglen] != '('))
		src = ddcci_capstr_tok(src+1, -1);
	if (!src || src[0] == '\0')
		return ERR_PTR(-ENOENT);

	/* Locate end of value */
	src += taglen+2;
	ptr = ddcci_capstr_tok(src, 0);
	if (unlikely(!ptr))
		return ERR_PTR(-EOVERFLOW);

	/* Check length of tag data */
	len = ptr-src;
	if (unlikely(len < 0 || len > 65535))
		return ERR_PTR(-EMSGSIZE);

	/* Return pointer and length */
	if (likely(length != NULL))
		*length = (size_t)len;
	return src;
}
EXPORT_SYMBOL(ddcci_find_capstr_item);

/* Search the capability string for a tag and copy the value to dest */
static int ddcci_cpy_capstr_item(char *dest, const char *src,
				  const char * __restrict tag, size_t maxlen)
{
	const char *ptr;
	size_t len;

	/* Find tag */
	ptr = ddcci_find_capstr_item(src, tag, &len);
	if (IS_ERR(ptr)) {
		return PTR_ERR(ptr);
	}

	/* Copy value */
	memcpy(dest, ptr, min(len, maxlen));
	return 0;
}

/* Fill fields in device by parsing the capability string */
static int ddcci_parse_capstring(struct ddcci_device *device)
{
	const char *capstr = device->capabilities;
	int ret = 0;

	if (!capstr)
		return -EINVAL;

	/* capability string start with a paren */
	if (capstr[0] != '(')
		return -EINVAL;

	/* get prot(...) */
	ret = ddcci_cpy_capstr_item(device->prot, capstr, "prot", sizeof(device->prot)-1);
	if (ret) {
		if (ret == -ENOENT) {
			dev_warn(&device->dev, "malformed capability string: no protocol tag");
			memset(device->prot, 0, sizeof(device->prot)-1);
		} else {
			return ret;
		}
	}

	/* get type(...) */
	ret = ddcci_cpy_capstr_item(device->type, capstr, "type", sizeof(device->type)-1);
	if (ret) {
		if (ret == -ENOENT) {
			dev_warn(&device->dev, "malformed capability string: no type tag");
			memset(device->type, 0, sizeof(device->type)-1);
		} else {
			return ret;
		}
	}

	/* and then model(...) */
	ret = ddcci_cpy_capstr_item(device->model, capstr, "model", sizeof(device->model)-1);
	if (ret) {
		if (ret == -ENOENT) {
			dev_warn(&device->dev, "malformed capability string: no model tag");
			memset(device->model, 0, sizeof(device->model)-1);
		} else {
			return ret;
		}
	}

	/* if there is no protocol tag */
	if (!device->prot[0]) {
		/* and no type tag: give up. */
		if (!device->type[0])
			return -ENOENT;

		/* Assume protocol "monitor" if type is "LCD" or "CRT" */
		if (strncasecmp(device->type, "LCD", sizeof(device->type)-1) == 0
		 || strncasecmp(device->type, "CRT", sizeof(device->type)-1) == 0) {
			memcpy(device->prot, "monitor", 7);
		}
	}

	/* skip the rest for now */

	return 0;
}

/* Probe for a device on an inner address and create a ddcci_device for it */
static int ddcci_detect_device(struct i2c_client *client, unsigned char addr,
			       int dependent)
{
	int ret;
	unsigned char outer_addr = client->addr << 1;
	unsigned char *buffer = NULL;
	struct ddcci_bus_drv_data *drv_data = i2c_get_clientdata(client);
	struct ddcci_device *device = NULL;

	mutex_lock(&drv_data->lock);

	/* Allocate buffer big enough for any capability string */
	buffer = kmalloc(16384, GFP_KERNEL);
	if (!buffer) {
		ret = -ENOMEM;
		goto err_end;
	}

	/* Allocate device struct */
	device = kzalloc(sizeof(struct ddcci_device), GFP_KERNEL);
	if (!device) {
		ret = -ENOMEM;
		goto err_end;
	}

	/* Initialize device */
	device_initialize(&device->dev);
	device->dev.parent = &client->dev;
	device->dev.bus = &ddcci_bus_type;
	device->outer_addr = outer_addr;
	device->inner_addr = addr;
	device->bus_drv_data = drv_data;
	device->i2c_client = client;

	if (!dependent) {
		device->dev.type = &ddcci_device_type;
		ret = dev_set_name(&device->dev, "ddcci%d", client->adapter->nr);
	} else if (outer_addr == dependent) {
		/* Internal dependent device */
		device->dev.type = &ddcci_dependent_type;
		device->flags = DDCCI_FLAG_DEPENDENT;
		ret = dev_set_name(&device->dev, "ddcci%di%02x", client->adapter->nr, addr);
	} else if (outer_addr == addr) {
		/* External dependent device */
		device->dev.type = &ddcci_dependent_type;
		device->flags = DDCCI_FLAG_DEPENDENT | DDCCI_FLAG_EXTERNAL;
		ret = dev_set_name(&device->dev, "ddcci%de%02x", client->adapter->nr, addr);
	} else {
		/* Dependent device of external dependent device
		   Just in case something like this exists */
		device->dev.type = &ddcci_dependent_type;
		device->flags = DDCCI_FLAG_DEPENDENT | DDCCI_FLAG_EXTERNAL;
		ret = dev_set_name(&device->dev, "ddcci%de%02x%02x", client->adapter->nr, outer_addr, addr);
	}

	if (ret)
		goto err_free;

	/* Read identification and check for quirks */
	ret = ddcci_identify_device(client, addr, buffer, 29);
	if (ret < 0) {
		if (!dependent && (ret == -EBADMSG || ret == -EMSGSIZE)) {
			dev_warn(&device->dev, "DDC/CI main device sent broken response on identification. Trying to detect solely based on capability information.\n");
		} else {
			goto err_free;
		}
	}

	if (ret == 29 && buffer[0] == DDCCI_REPLY_ID) {
		memcpy(device->vendor, &buffer[7], 8);
		memcpy(device->module, &buffer[17], 8);
		device->device_number = be32_to_cpu(*(__force __be32 *)&buffer[18]);
	}

	/* Read capabilities */
	ret = ddcci_get_caps(client, addr, buffer, 16384);
	if (ret > 0) {
		/* Fixup unparenthesized capability strings, but only if the first
		   character is an ascii lower case letter.
		   This should still allow an early exit for completely garbled
		   data but helps detecting devices where only the parentheses are
		   missing, as the second char must be the first character of a
		   keyword. */
		if (ret > 2 && buffer[0] >= 'a' && buffer[0] <= 'z') {
			dev_err(&device->dev, "DDC/CI device quirk detected: unparenthesized capability string\n");
			device->capabilities = kzalloc(ret+3, GFP_KERNEL);
			if (!device->capabilities) {
				ret = -ENOMEM;
				goto err_free;
			}
			device->capabilities_len = ret+2;
			memcpy(&(device->capabilities[1]), buffer, ret);
			device->capabilities[0] = '(';
			device->capabilities[ret+1] = ')';
		} else {
			/* Standard case: simply copy the received string */
			device->capabilities = kzalloc(ret+1, GFP_KERNEL);
			if (!device->capabilities) {
				ret = -ENOMEM;
				goto err_free;
			}
			device->capabilities_len = ret;
			memcpy(device->capabilities, buffer, ret);
		}

		ret = ddcci_parse_capstring(device);
		if (ret) {
			dev_err(&device->dev, "malformed capability string: \"%s\" errno %d\n", device->capabilities, ret);
			ret = -EINVAL;
			goto err_free;
		}
	}

	/* Found a device if either identification or capabilities succeeded */
	if (!device->capabilities && device->vendor[0] == '\0') {
		dev_dbg(&client->dev,
			"[%02x:%02x] got neither valid identification nor capability data\n",
			client->addr << 1, addr);
		ret = -ENODEV;
		goto err_free;
	}

	/* Setup chardev */
	mutex_lock(&core_lock);
	ret = ddcci_setup_char_device(device);
	mutex_unlock(&core_lock);
	if (ret)
		goto err_free;

	/* Release mutex and add device to the tree */
	mutex_unlock(&drv_data->lock);
	pr_debug("found device at %d:%02x:%02x\n", client->adapter->nr, outer_addr, addr);
	ret = device_add(&device->dev);
	if (ret)
		goto err_free;

	goto end;
err_free:
	put_device(&device->dev);
err_end:
	mutex_unlock(&drv_data->lock);
end:
	kfree(buffer);
	return ret;
}

/* I2C detect function: check if a main or external dependent device exists */
static int ddcci_detect(struct i2c_client *client, struct i2c_board_info *info)
{
	int ret;
	unsigned char outer_addr;
	unsigned char inner_addr;
	unsigned char buf[32];
	unsigned char cmd_id[1] = { DDCCI_COMMAND_ID };
	unsigned char cmd_caps[3] = { DDCCI_COMMAND_CAPS, 0x00, 0x00};
	unsigned char *cmd;
	unsigned int cmd_len;

	/* Check for i2c_master_* functionality */
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		pr_debug("i2c adapter %d unsuitable: no i2c_master functionality\n", client->adapter->nr);
		return -ENODEV;
	}

	/* send Capabilities Request (for main) or Identification Request command (for dependent devices) */
	outer_addr = client->addr << 1;
	inner_addr = (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? DDCCI_HOST_ADDR_ODD : outer_addr | 1;
	cmd = (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? cmd_caps : cmd_id;
	cmd_len = (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? sizeof(cmd_caps) : sizeof(cmd_id);
	pr_debug("detecting %d:%02x\n", client->adapter->nr, outer_addr);

	ret = __ddcci_write_block(client, inner_addr, buf, true, cmd, cmd_len);

	if (ret == -ENXIO || ret == -EIO) {
		if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) {
			pr_debug("i2c write failed with ENXIO or EIO but bytewise writing is not supported\n");
			return -ENODEV;
		}
		pr_debug("i2c write failed with ENXIO or EIO, trying bytewise writing\n");
		ret = __ddcci_write_bytewise(client, inner_addr, true, cmd, cmd_len);
		if (ret == 0) {
			msleep(delay);
			ret = __ddcci_write_bytewise(client, inner_addr, true, cmd, cmd_len);
		}
	}

	if (ret < 0)
		return -ENODEV;

	/* wait for device */
	msleep(delay);
	/* receive answer */
	ret = i2c_master_recv(client, buf, 32);
	if (ret < 3) {
		pr_debug("detection failed: no answer\n");
		return -ENODEV;
	}

	/* check response starts with outer addr */
	if (buf[0] != outer_addr) {
		pr_debug("detection failed: invalid %s response (%02x != %02x)\n", (cmd == cmd_id) ? "identification" : "capabilities", buf[0], outer_addr);
		pr_debug("received message was %*ph \n", ret, buf);
		return -ENODEV;
	}

	pr_debug("detected %d:%02x\n", client->adapter->nr, outer_addr);

	/* set device type */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
	strscpy(info->type, (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? "ddcci" : "ddcci-dependent", I2C_NAME_SIZE);
#else
	strlcpy(info->type, (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? "ddcci" : "ddcci-dependent", I2C_NAME_SIZE);
#endif

	return 0;
}

/* I2C probe function */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
static int ddcci_probe(struct i2c_client *client)
{
	const struct i2c_device_id *id = i2c_client_get_device_id(client);
#else
static int ddcci_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
#endif
	int i, ret = -ENODEV, tmp;
	unsigned char main_addr, addr;
	struct ddcci_bus_drv_data *drv_data;

	/* Initialize driver data structure */
	drv_data = devm_kzalloc(&client->dev, sizeof(struct ddcci_bus_drv_data), GFP_KERNEL);
	if (!drv_data)
		return -ENOMEM;
	drv_data->i2c_dev = client;
	mutex_init(&drv_data->lock);

	/* Set i2c client data */
	i2c_set_clientdata(client, drv_data);

	if (id->driver_data == 0) {
		/* Core device, probe at 0x6E */
		main_addr = DDCCI_DEFAULT_DEVICE_ADDR;
		dev_dbg(&client->dev, "probing core device [%02x]\n",
			client->addr << 1);
		ret = ddcci_detect_device(client, main_addr, 0);
		if (ret) {
			dev_info(&client->dev, "core device [%02x] probe failed: %d\n",
				 client->addr << 1, ret);
			if (ret == -EIO)
				ret = -ENODEV;
			goto err_free;
		}

		/* Detect internal dependent devices */
		dev_dbg(&client->dev, "probing internal dependent devices\n");
		for (i = 0; i < autoprobe_addr_count; ++i) {
			addr = (unsigned short)autoprobe_addrs[i];
			if ((addr & 1) == 0 && addr != main_addr) {
				tmp = ddcci_detect_device(client, addr, main_addr);
				if (tmp < 0 && tmp != -ENODEV) {
					dev_info(&client->dev, "internal dependent device [%02x:%02x] probe failed: %d\n",
						 client->addr << 1, addr, ret);
				}
			}
		}
	} else if (id->driver_data == 1) {
		/* External dependent device */
		main_addr = client->addr << 1;
		dev_dbg(&client->dev, "probing external dependent device [%02x]\n", main_addr);
		ret = ddcci_detect_device(client, main_addr, -1);
		if (ret) {
			dev_info(&client->dev, "external dependent device [%02x] probe failed: %d\n",
				 main_addr, ret);
			if (ret == -EIO)
				ret = -ENODEV;
			goto err_free;
		}
	} else {
		dev_warn(&client->dev,
			 "probe() called with invalid i2c device id\n");
		ret = -EINVAL;
	}

	goto end;
err_free:
	devm_kfree(&client->dev, drv_data);
end:
	return ret;
}

/*
 * Callback for bus_find_device() used in ddcci_remove()
 *
 * Find next device on i2c_client not flagged with
 * DDCCI_FLAG_REMOVED and flag it.
 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
static int ddcci_remove_helper(struct device *dev, const void *p)
#else
static int ddcci_remove_helper(struct device *dev, void *p)
#endif
{
	struct ddcci_device *device;

	device = ddcci_verify_device(dev);
	if (!device || device->flags & DDCCI_FLAG_REMOVED)
		return 0;

	if (!p || (dev->parent == p)) {
		device->flags |= DDCCI_FLAG_REMOVED;
		wmb();
		return 1;
	}

	return 0;
}

/* I2C driver remove callback: unregister all subdevices */
static int ddcci_remove(struct i2c_client *client)
{
	struct ddcci_bus_drv_data *drv_data = i2c_get_clientdata(client);
	struct device *dev;

	mutex_lock(&drv_data->lock);
	while (1) {
		dev = bus_find_device(&ddcci_bus_type, NULL, client,
				      ddcci_remove_helper);
		if (!dev)
			break;
		device_unregister(dev);
		put_device(dev);
	}
	mutex_unlock(&drv_data->lock);
	return 0;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
static void ddcci_remove_void(struct i2c_client *client)
{
	ddcci_remove(client);
}
#endif

/*
 * I2C driver device identification table.
 */
static const struct i2c_device_id ddcci_idtable[] = {
	{ "ddcci", 0 },
	{ "ddcci-dependent", 1 },
	{}
};
MODULE_DEVICE_TABLE(i2c, ddcci_idtable);

/*
 * I2C driver description structure
 */
static struct i2c_driver ddcci_driver = {
	.driver = {
		.name	= "ddcci",
		.owner	= THIS_MODULE,
	},

	.id_table	= ddcci_idtable,
	.probe		= ddcci_probe,
	#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
	.remove		= ddcci_remove_void,
	#else
	.remove		= ddcci_remove,
	#endif
	#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 8, 0)
	.class		= I2C_CLASS_DDC,
	#endif
	.detect		= ddcci_detect,
	.address_list	= I2C_ADDRS(
		DDCCI_DEFAULT_DEVICE_ADDR>>1
	),
};

/*
 * Module initialization function. Called when the module is inserted or
 * (if builtin) at boot time.
 */
static int __init ddcci_module_init(void)
{
	int ret;

	pr_debug("initializing ddcci driver\n");

	#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
	pr_warn("WARNING: Auto-probing of displays is not available on kernel 6.8 and later\n");
	#endif

	/* Allocate a device number region for the character devices */
	ret = alloc_chrdev_region(&ddcci_cdev_first, 0, 128, DEVICE_NAME);
	if (ret < 0) {
		pr_err("failed to register device region: error %d\n", ret);
		goto err_chrdevreg;
	}
	ddcci_cdev_next = ddcci_cdev_first;
	ddcci_cdev_end = MKDEV(MAJOR(ddcci_cdev_first), MINOR(ddcci_cdev_first)+128);

	/* Register bus */
	ret = bus_register(&ddcci_bus_type);
	if (ret) {
		pr_err("failed to register bus 'ddcci'\n");
		goto err_busreg;
	}

	/* Register I2C driver */
	ret = i2c_add_driver(&ddcci_driver);
	if (ret) {
		pr_err("failed to register i2c driver\n");
		goto err_drvreg;
	}

	pr_debug("ddcci driver initialized\n");
	smp_store_release(&module_initialized, true);

	return 0;

err_drvreg:
	bus_unregister(&ddcci_bus_type);
err_busreg:
	unregister_chrdev_region(ddcci_cdev_first, 128);
err_chrdevreg:
	return ret;
}

/*
 * Module clean-up function. Called when the module is removed.
 */
static void __exit ddcci_module_exit(void)
{
	struct device *dev;
	smp_store_release(&module_initialized, false);

	while (1) {
		dev = bus_find_device(&ddcci_bus_type, NULL, NULL, ddcci_remove_helper);
		if (!dev)
			break;
		device_unregister(dev);
		put_device(dev);
	}

	i2c_del_driver(&ddcci_driver);
	bus_unregister(&ddcci_bus_type);
	unregister_chrdev_region(ddcci_cdev_first, 128);
}

/* Let the kernel know the calls for module init and exit */
module_init(ddcci_module_init);
module_exit(ddcci_module_exit);

/* Module parameter description */
module_param(delay, uint, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(delay, "default delay after bus writes (in ms, default 60)");
module_param_array(autoprobe_addrs, ushort, &autoprobe_addr_count, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(autoprobe_addrs, "internal dependent device addresses to autoprobe");

/* Module description */
MODULE_AUTHOR("Christoph Grenz");
MODULE_DESCRIPTION("DDC/CI bus driver");
MODULE_VERSION("0.4.5");
MODULE_LICENSE("GPL");