hid-cp2112.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge
  3. * Copyright (c) 2013,2014 Uplogix, Inc.
  4. * David Barksdale <dbarksdale@uplogix.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. /*
  16. * The Silicon Labs CP2112 chip is a USB HID device which provides an
  17. * SMBus controller for talking to slave devices and 8 GPIO pins. The
  18. * host communicates with the CP2112 via raw HID reports.
  19. *
  20. * Data Sheet:
  21. * http://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf
  22. * Programming Interface Specification:
  23. * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN495.pdf
  24. */
  25. #include <linux/gpio.h>
  26. #include <linux/hid.h>
  27. #include <linux/i2c.h>
  28. #include <linux/module.h>
  29. #include <linux/nls.h>
  30. #include <linux/usb/ch9.h>
  31. #include "hid-ids.h"
  32. enum {
  33. CP2112_GPIO_CONFIG = 0x02,
  34. CP2112_GPIO_GET = 0x03,
  35. CP2112_GPIO_SET = 0x04,
  36. CP2112_GET_VERSION_INFO = 0x05,
  37. CP2112_SMBUS_CONFIG = 0x06,
  38. CP2112_DATA_READ_REQUEST = 0x10,
  39. CP2112_DATA_WRITE_READ_REQUEST = 0x11,
  40. CP2112_DATA_READ_FORCE_SEND = 0x12,
  41. CP2112_DATA_READ_RESPONSE = 0x13,
  42. CP2112_DATA_WRITE_REQUEST = 0x14,
  43. CP2112_TRANSFER_STATUS_REQUEST = 0x15,
  44. CP2112_TRANSFER_STATUS_RESPONSE = 0x16,
  45. CP2112_CANCEL_TRANSFER = 0x17,
  46. CP2112_LOCK_BYTE = 0x20,
  47. CP2112_USB_CONFIG = 0x21,
  48. CP2112_MANUFACTURER_STRING = 0x22,
  49. CP2112_PRODUCT_STRING = 0x23,
  50. CP2112_SERIAL_STRING = 0x24,
  51. };
  52. enum {
  53. STATUS0_IDLE = 0x00,
  54. STATUS0_BUSY = 0x01,
  55. STATUS0_COMPLETE = 0x02,
  56. STATUS0_ERROR = 0x03,
  57. };
  58. enum {
  59. STATUS1_TIMEOUT_NACK = 0x00,
  60. STATUS1_TIMEOUT_BUS = 0x01,
  61. STATUS1_ARBITRATION_LOST = 0x02,
  62. STATUS1_READ_INCOMPLETE = 0x03,
  63. STATUS1_WRITE_INCOMPLETE = 0x04,
  64. STATUS1_SUCCESS = 0x05,
  65. };
  66. struct cp2112_smbus_config_report {
  67. u8 report; /* CP2112_SMBUS_CONFIG */
  68. __be32 clock_speed; /* Hz */
  69. u8 device_address; /* Stored in the upper 7 bits */
  70. u8 auto_send_read; /* 1 = enabled, 0 = disabled */
  71. __be16 write_timeout; /* ms, 0 = no timeout */
  72. __be16 read_timeout; /* ms, 0 = no timeout */
  73. u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */
  74. __be16 retry_time; /* # of retries, 0 = no limit */
  75. } __packed;
  76. struct cp2112_usb_config_report {
  77. u8 report; /* CP2112_USB_CONFIG */
  78. __le16 vid; /* Vendor ID */
  79. __le16 pid; /* Product ID */
  80. u8 max_power; /* Power requested in 2mA units */
  81. u8 power_mode; /* 0x00 = bus powered
  82. 0x01 = self powered & regulator off
  83. 0x02 = self powered & regulator on */
  84. u8 release_major;
  85. u8 release_minor;
  86. u8 mask; /* What fields to program */
  87. } __packed;
  88. struct cp2112_read_req_report {
  89. u8 report; /* CP2112_DATA_READ_REQUEST */
  90. u8 slave_address;
  91. __be16 length;
  92. } __packed;
  93. struct cp2112_write_read_req_report {
  94. u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */
  95. u8 slave_address;
  96. __be16 length;
  97. u8 target_address_length;
  98. u8 target_address[16];
  99. } __packed;
  100. struct cp2112_write_req_report {
  101. u8 report; /* CP2112_DATA_WRITE_REQUEST */
  102. u8 slave_address;
  103. u8 length;
  104. u8 data[61];
  105. } __packed;
  106. struct cp2112_force_read_report {
  107. u8 report; /* CP2112_DATA_READ_FORCE_SEND */
  108. __be16 length;
  109. } __packed;
  110. struct cp2112_xfer_status_report {
  111. u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */
  112. u8 status0; /* STATUS0_* */
  113. u8 status1; /* STATUS1_* */
  114. __be16 retries;
  115. __be16 length;
  116. } __packed;
  117. struct cp2112_string_report {
  118. u8 dummy; /* force .string to be aligned */
  119. u8 report; /* CP2112_*_STRING */
  120. u8 length; /* length in bytes of everyting after .report */
  121. u8 type; /* USB_DT_STRING */
  122. wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */
  123. } __packed;
  124. /* Number of times to request transfer status before giving up waiting for a
  125. transfer to complete. This may need to be changed if SMBUS clock, retries,
  126. or read/write/scl_low timeout settings are changed. */
  127. static const int XFER_STATUS_RETRIES = 10;
  128. /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
  129. CP2112_TRANSFER_STATUS_RESPONSE. */
  130. static const int RESPONSE_TIMEOUT = 50;
  131. static const struct hid_device_id cp2112_devices[] = {
  132. { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
  133. { }
  134. };
  135. MODULE_DEVICE_TABLE(hid, cp2112_devices);
  136. struct cp2112_device {
  137. struct i2c_adapter adap;
  138. struct hid_device *hdev;
  139. wait_queue_head_t wait;
  140. u8 read_data[61];
  141. u8 read_length;
  142. int xfer_status;
  143. atomic_t read_avail;
  144. atomic_t xfer_avail;
  145. struct gpio_chip gc;
  146. };
  147. static int gpio_push_pull = 0xFF;
  148. module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR);
  149. MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
  150. static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  151. {
  152. struct cp2112_device *dev = container_of(chip, struct cp2112_device,
  153. gc);
  154. struct hid_device *hdev = dev->hdev;
  155. u8 buf[5];
  156. int ret;
  157. ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
  158. sizeof(buf), HID_FEATURE_REPORT,
  159. HID_REQ_GET_REPORT);
  160. if (ret != sizeof(buf)) {
  161. hid_err(hdev, "error requesting GPIO config: %d\n", ret);
  162. return ret;
  163. }
  164. buf[1] &= ~(1 << offset);
  165. buf[2] = gpio_push_pull;
  166. ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
  167. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  168. if (ret < 0) {
  169. hid_err(hdev, "error setting GPIO config: %d\n", ret);
  170. return ret;
  171. }
  172. return 0;
  173. }
  174. static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  175. {
  176. struct cp2112_device *dev = container_of(chip, struct cp2112_device,
  177. gc);
  178. struct hid_device *hdev = dev->hdev;
  179. u8 buf[3];
  180. int ret;
  181. buf[0] = CP2112_GPIO_SET;
  182. buf[1] = value ? 0xff : 0;
  183. buf[2] = 1 << offset;
  184. ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf, sizeof(buf),
  185. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  186. if (ret < 0)
  187. hid_err(hdev, "error setting GPIO values: %d\n", ret);
  188. }
  189. static int cp2112_gpio_get(struct gpio_chip *chip, unsigned offset)
  190. {
  191. struct cp2112_device *dev = container_of(chip, struct cp2112_device,
  192. gc);
  193. struct hid_device *hdev = dev->hdev;
  194. u8 buf[2];
  195. int ret;
  196. ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf, sizeof(buf),
  197. HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
  198. if (ret != sizeof(buf)) {
  199. hid_err(hdev, "error requesting GPIO values: %d\n", ret);
  200. return ret;
  201. }
  202. return (buf[1] >> offset) & 1;
  203. }
  204. static int cp2112_gpio_direction_output(struct gpio_chip *chip,
  205. unsigned offset, int value)
  206. {
  207. struct cp2112_device *dev = container_of(chip, struct cp2112_device,
  208. gc);
  209. struct hid_device *hdev = dev->hdev;
  210. u8 buf[5];
  211. int ret;
  212. ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
  213. sizeof(buf), HID_FEATURE_REPORT,
  214. HID_REQ_GET_REPORT);
  215. if (ret != sizeof(buf)) {
  216. hid_err(hdev, "error requesting GPIO config: %d\n", ret);
  217. return ret;
  218. }
  219. buf[1] |= 1 << offset;
  220. buf[2] = gpio_push_pull;
  221. ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
  222. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  223. if (ret < 0) {
  224. hid_err(hdev, "error setting GPIO config: %d\n", ret);
  225. return ret;
  226. }
  227. /*
  228. * Set gpio value when output direction is already set,
  229. * as specified in AN495, Rev. 0.2, cpt. 4.4
  230. */
  231. cp2112_gpio_set(chip, offset, value);
  232. return 0;
  233. }
  234. static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
  235. u8 *data, size_t count, unsigned char report_type)
  236. {
  237. u8 *buf;
  238. int ret;
  239. buf = kmalloc(count, GFP_KERNEL);
  240. if (!buf)
  241. return -ENOMEM;
  242. ret = hid_hw_raw_request(hdev, report_number, buf, count,
  243. report_type, HID_REQ_GET_REPORT);
  244. memcpy(data, buf, count);
  245. kfree(buf);
  246. return ret;
  247. }
  248. static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
  249. unsigned char report_type)
  250. {
  251. u8 *buf;
  252. int ret;
  253. buf = kmemdup(data, count, GFP_KERNEL);
  254. if (!buf)
  255. return -ENOMEM;
  256. if (report_type == HID_OUTPUT_REPORT)
  257. ret = hid_hw_output_report(hdev, buf, count);
  258. else
  259. ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
  260. HID_REQ_SET_REPORT);
  261. kfree(buf);
  262. return ret;
  263. }
  264. static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
  265. {
  266. int ret = 0;
  267. /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
  268. * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
  269. * come in cp2112_raw_event or timeout. There will only be one of these
  270. * in flight at any one time. The timeout is extremely large and is a
  271. * last resort if the CP2112 has died. If we do timeout we don't expect
  272. * to receive the response which would cause data races, it's not like
  273. * we can do anything about it anyway.
  274. */
  275. ret = wait_event_interruptible_timeout(dev->wait,
  276. atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
  277. if (-ERESTARTSYS == ret)
  278. return ret;
  279. if (!ret)
  280. return -ETIMEDOUT;
  281. atomic_set(avail, 0);
  282. return 0;
  283. }
  284. static int cp2112_xfer_status(struct cp2112_device *dev)
  285. {
  286. struct hid_device *hdev = dev->hdev;
  287. u8 buf[2];
  288. int ret;
  289. buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
  290. buf[1] = 0x01;
  291. atomic_set(&dev->xfer_avail, 0);
  292. ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
  293. if (ret < 0) {
  294. hid_warn(hdev, "Error requesting status: %d\n", ret);
  295. return ret;
  296. }
  297. ret = cp2112_wait(dev, &dev->xfer_avail);
  298. if (ret)
  299. return ret;
  300. return dev->xfer_status;
  301. }
  302. static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
  303. {
  304. struct hid_device *hdev = dev->hdev;
  305. struct cp2112_force_read_report report;
  306. int ret;
  307. report.report = CP2112_DATA_READ_FORCE_SEND;
  308. report.length = cpu_to_be16(size);
  309. atomic_set(&dev->read_avail, 0);
  310. ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
  311. HID_OUTPUT_REPORT);
  312. if (ret < 0) {
  313. hid_warn(hdev, "Error requesting data: %d\n", ret);
  314. return ret;
  315. }
  316. ret = cp2112_wait(dev, &dev->read_avail);
  317. if (ret)
  318. return ret;
  319. hid_dbg(hdev, "read %d of %zd bytes requested\n",
  320. dev->read_length, size);
  321. if (size > dev->read_length)
  322. size = dev->read_length;
  323. memcpy(data, dev->read_data, size);
  324. return dev->read_length;
  325. }
  326. static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
  327. {
  328. struct cp2112_read_req_report *report = buf;
  329. if (length < 1 || length > 512)
  330. return -EINVAL;
  331. report->report = CP2112_DATA_READ_REQUEST;
  332. report->slave_address = slave_address << 1;
  333. report->length = cpu_to_be16(length);
  334. return sizeof(*report);
  335. }
  336. static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
  337. u8 command, u8 *data, u8 data_length)
  338. {
  339. struct cp2112_write_read_req_report *report = buf;
  340. if (length < 1 || length > 512
  341. || data_length > sizeof(report->target_address) - 1)
  342. return -EINVAL;
  343. report->report = CP2112_DATA_WRITE_READ_REQUEST;
  344. report->slave_address = slave_address << 1;
  345. report->length = cpu_to_be16(length);
  346. report->target_address_length = data_length + 1;
  347. report->target_address[0] = command;
  348. memcpy(&report->target_address[1], data, data_length);
  349. return data_length + 6;
  350. }
  351. static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
  352. u8 data_length)
  353. {
  354. struct cp2112_write_req_report *report = buf;
  355. if (data_length > sizeof(report->data) - 1)
  356. return -EINVAL;
  357. report->report = CP2112_DATA_WRITE_REQUEST;
  358. report->slave_address = slave_address << 1;
  359. report->length = data_length + 1;
  360. report->data[0] = command;
  361. memcpy(&report->data[1], data, data_length);
  362. return data_length + 4;
  363. }
  364. static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data,
  365. u8 data_length)
  366. {
  367. struct cp2112_write_req_report *report = buf;
  368. if (data_length > sizeof(report->data))
  369. return -EINVAL;
  370. report->report = CP2112_DATA_WRITE_REQUEST;
  371. report->slave_address = slave_address << 1;
  372. report->length = data_length;
  373. memcpy(report->data, data, data_length);
  374. return data_length + 3;
  375. }
  376. static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  377. int num)
  378. {
  379. struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
  380. struct hid_device *hdev = dev->hdev;
  381. u8 buf[64];
  382. ssize_t count;
  383. unsigned int retries;
  384. int ret;
  385. hid_dbg(hdev, "I2C %d messages\n", num);
  386. if (num != 1) {
  387. hid_err(hdev,
  388. "Multi-message I2C transactions not supported\n");
  389. return -EOPNOTSUPP;
  390. }
  391. if (msgs->flags & I2C_M_RD)
  392. count = cp2112_read_req(buf, msgs->addr, msgs->len);
  393. else
  394. count = cp2112_i2c_write_req(buf, msgs->addr, msgs->buf,
  395. msgs->len);
  396. if (count < 0)
  397. return count;
  398. ret = hid_hw_power(hdev, PM_HINT_FULLON);
  399. if (ret < 0) {
  400. hid_err(hdev, "power management error: %d\n", ret);
  401. return ret;
  402. }
  403. ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
  404. if (ret < 0) {
  405. hid_warn(hdev, "Error starting transaction: %d\n", ret);
  406. goto power_normal;
  407. }
  408. for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
  409. ret = cp2112_xfer_status(dev);
  410. if (-EBUSY == ret)
  411. continue;
  412. if (ret < 0)
  413. goto power_normal;
  414. break;
  415. }
  416. if (XFER_STATUS_RETRIES <= retries) {
  417. hid_warn(hdev, "Transfer timed out, cancelling.\n");
  418. buf[0] = CP2112_CANCEL_TRANSFER;
  419. buf[1] = 0x01;
  420. ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
  421. if (ret < 0)
  422. hid_warn(hdev, "Error cancelling transaction: %d\n",
  423. ret);
  424. ret = -ETIMEDOUT;
  425. goto power_normal;
  426. }
  427. if (!(msgs->flags & I2C_M_RD))
  428. goto finish;
  429. ret = cp2112_read(dev, msgs->buf, msgs->len);
  430. if (ret < 0)
  431. goto power_normal;
  432. if (ret != msgs->len) {
  433. hid_warn(hdev, "short read: %d < %d\n", ret, msgs->len);
  434. ret = -EIO;
  435. goto power_normal;
  436. }
  437. finish:
  438. /* return the number of transferred messages */
  439. ret = 1;
  440. power_normal:
  441. hid_hw_power(hdev, PM_HINT_NORMAL);
  442. hid_dbg(hdev, "I2C transfer finished: %d\n", ret);
  443. return ret;
  444. }
  445. static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
  446. unsigned short flags, char read_write, u8 command,
  447. int size, union i2c_smbus_data *data)
  448. {
  449. struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
  450. struct hid_device *hdev = dev->hdev;
  451. u8 buf[64];
  452. __be16 word;
  453. ssize_t count;
  454. size_t read_length = 0;
  455. unsigned int retries;
  456. int ret;
  457. hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
  458. read_write == I2C_SMBUS_WRITE ? "write" : "read",
  459. addr, flags, command, size);
  460. switch (size) {
  461. case I2C_SMBUS_BYTE:
  462. read_length = 1;
  463. if (I2C_SMBUS_READ == read_write)
  464. count = cp2112_read_req(buf, addr, read_length);
  465. else
  466. count = cp2112_write_req(buf, addr, data->byte, NULL,
  467. 0);
  468. break;
  469. case I2C_SMBUS_BYTE_DATA:
  470. read_length = 1;
  471. if (I2C_SMBUS_READ == read_write)
  472. count = cp2112_write_read_req(buf, addr, read_length,
  473. command, NULL, 0);
  474. else
  475. count = cp2112_write_req(buf, addr, command,
  476. &data->byte, 1);
  477. break;
  478. case I2C_SMBUS_WORD_DATA:
  479. read_length = 2;
  480. word = cpu_to_be16(data->word);
  481. if (I2C_SMBUS_READ == read_write)
  482. count = cp2112_write_read_req(buf, addr, read_length,
  483. command, NULL, 0);
  484. else
  485. count = cp2112_write_req(buf, addr, command,
  486. (u8 *)&word, 2);
  487. break;
  488. case I2C_SMBUS_PROC_CALL:
  489. size = I2C_SMBUS_WORD_DATA;
  490. read_write = I2C_SMBUS_READ;
  491. read_length = 2;
  492. word = cpu_to_be16(data->word);
  493. count = cp2112_write_read_req(buf, addr, read_length, command,
  494. (u8 *)&word, 2);
  495. break;
  496. case I2C_SMBUS_I2C_BLOCK_DATA:
  497. size = I2C_SMBUS_BLOCK_DATA;
  498. /* fallthrough */
  499. case I2C_SMBUS_BLOCK_DATA:
  500. if (I2C_SMBUS_READ == read_write) {
  501. count = cp2112_write_read_req(buf, addr,
  502. I2C_SMBUS_BLOCK_MAX,
  503. command, NULL, 0);
  504. } else {
  505. count = cp2112_write_req(buf, addr, command,
  506. data->block,
  507. data->block[0] + 1);
  508. }
  509. break;
  510. case I2C_SMBUS_BLOCK_PROC_CALL:
  511. size = I2C_SMBUS_BLOCK_DATA;
  512. read_write = I2C_SMBUS_READ;
  513. count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
  514. command, data->block,
  515. data->block[0] + 1);
  516. break;
  517. default:
  518. hid_warn(hdev, "Unsupported transaction %d\n", size);
  519. return -EOPNOTSUPP;
  520. }
  521. if (count < 0)
  522. return count;
  523. ret = hid_hw_power(hdev, PM_HINT_FULLON);
  524. if (ret < 0) {
  525. hid_err(hdev, "power management error: %d\n", ret);
  526. return ret;
  527. }
  528. ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
  529. if (ret < 0) {
  530. hid_warn(hdev, "Error starting transaction: %d\n", ret);
  531. goto power_normal;
  532. }
  533. for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
  534. ret = cp2112_xfer_status(dev);
  535. if (-EBUSY == ret)
  536. continue;
  537. if (ret < 0)
  538. goto power_normal;
  539. break;
  540. }
  541. if (XFER_STATUS_RETRIES <= retries) {
  542. hid_warn(hdev, "Transfer timed out, cancelling.\n");
  543. buf[0] = CP2112_CANCEL_TRANSFER;
  544. buf[1] = 0x01;
  545. ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
  546. if (ret < 0)
  547. hid_warn(hdev, "Error cancelling transaction: %d\n",
  548. ret);
  549. ret = -ETIMEDOUT;
  550. goto power_normal;
  551. }
  552. if (I2C_SMBUS_WRITE == read_write) {
  553. ret = 0;
  554. goto power_normal;
  555. }
  556. if (I2C_SMBUS_BLOCK_DATA == size)
  557. read_length = ret;
  558. ret = cp2112_read(dev, buf, read_length);
  559. if (ret < 0)
  560. goto power_normal;
  561. if (ret != read_length) {
  562. hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
  563. ret = -EIO;
  564. goto power_normal;
  565. }
  566. switch (size) {
  567. case I2C_SMBUS_BYTE:
  568. case I2C_SMBUS_BYTE_DATA:
  569. data->byte = buf[0];
  570. break;
  571. case I2C_SMBUS_WORD_DATA:
  572. data->word = be16_to_cpup((__be16 *)buf);
  573. break;
  574. case I2C_SMBUS_BLOCK_DATA:
  575. if (read_length > I2C_SMBUS_BLOCK_MAX) {
  576. ret = -EPROTO;
  577. goto power_normal;
  578. }
  579. memcpy(data->block, buf, read_length);
  580. break;
  581. }
  582. ret = 0;
  583. power_normal:
  584. hid_hw_power(hdev, PM_HINT_NORMAL);
  585. hid_dbg(hdev, "transfer finished: %d\n", ret);
  586. return ret;
  587. }
  588. static u32 cp2112_functionality(struct i2c_adapter *adap)
  589. {
  590. return I2C_FUNC_I2C |
  591. I2C_FUNC_SMBUS_BYTE |
  592. I2C_FUNC_SMBUS_BYTE_DATA |
  593. I2C_FUNC_SMBUS_WORD_DATA |
  594. I2C_FUNC_SMBUS_BLOCK_DATA |
  595. I2C_FUNC_SMBUS_I2C_BLOCK |
  596. I2C_FUNC_SMBUS_PROC_CALL |
  597. I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
  598. }
  599. static const struct i2c_algorithm smbus_algorithm = {
  600. .master_xfer = cp2112_i2c_xfer,
  601. .smbus_xfer = cp2112_xfer,
  602. .functionality = cp2112_functionality,
  603. };
  604. static int cp2112_get_usb_config(struct hid_device *hdev,
  605. struct cp2112_usb_config_report *cfg)
  606. {
  607. int ret;
  608. ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
  609. HID_FEATURE_REPORT);
  610. if (ret != sizeof(*cfg)) {
  611. hid_err(hdev, "error reading usb config: %d\n", ret);
  612. if (ret < 0)
  613. return ret;
  614. return -EIO;
  615. }
  616. return 0;
  617. }
  618. static int cp2112_set_usb_config(struct hid_device *hdev,
  619. struct cp2112_usb_config_report *cfg)
  620. {
  621. int ret;
  622. BUG_ON(cfg->report != CP2112_USB_CONFIG);
  623. ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
  624. HID_FEATURE_REPORT);
  625. if (ret != sizeof(*cfg)) {
  626. hid_err(hdev, "error writing usb config: %d\n", ret);
  627. if (ret < 0)
  628. return ret;
  629. return -EIO;
  630. }
  631. return 0;
  632. }
  633. static void chmod_sysfs_attrs(struct hid_device *hdev);
  634. #define CP2112_CONFIG_ATTR(name, store, format, ...) \
  635. static ssize_t name##_store(struct device *kdev, \
  636. struct device_attribute *attr, const char *buf, \
  637. size_t count) \
  638. { \
  639. struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
  640. struct cp2112_usb_config_report cfg; \
  641. int ret = cp2112_get_usb_config(hdev, &cfg); \
  642. if (ret) \
  643. return ret; \
  644. store; \
  645. ret = cp2112_set_usb_config(hdev, &cfg); \
  646. if (ret) \
  647. return ret; \
  648. chmod_sysfs_attrs(hdev); \
  649. return count; \
  650. } \
  651. static ssize_t name##_show(struct device *kdev, \
  652. struct device_attribute *attr, char *buf) \
  653. { \
  654. struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
  655. struct cp2112_usb_config_report cfg; \
  656. int ret = cp2112_get_usb_config(hdev, &cfg); \
  657. if (ret) \
  658. return ret; \
  659. return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
  660. } \
  661. static DEVICE_ATTR_RW(name);
  662. CP2112_CONFIG_ATTR(vendor_id, ({
  663. u16 vid;
  664. if (sscanf(buf, "%hi", &vid) != 1)
  665. return -EINVAL;
  666. cfg.vid = cpu_to_le16(vid);
  667. cfg.mask = 0x01;
  668. }), "0x%04x\n", le16_to_cpu(cfg.vid));
  669. CP2112_CONFIG_ATTR(product_id, ({
  670. u16 pid;
  671. if (sscanf(buf, "%hi", &pid) != 1)
  672. return -EINVAL;
  673. cfg.pid = cpu_to_le16(pid);
  674. cfg.mask = 0x02;
  675. }), "0x%04x\n", le16_to_cpu(cfg.pid));
  676. CP2112_CONFIG_ATTR(max_power, ({
  677. int mA;
  678. if (sscanf(buf, "%i", &mA) != 1)
  679. return -EINVAL;
  680. cfg.max_power = (mA + 1) / 2;
  681. cfg.mask = 0x04;
  682. }), "%u mA\n", cfg.max_power * 2);
  683. CP2112_CONFIG_ATTR(power_mode, ({
  684. if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
  685. return -EINVAL;
  686. cfg.mask = 0x08;
  687. }), "%u\n", cfg.power_mode);
  688. CP2112_CONFIG_ATTR(release_version, ({
  689. if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
  690. != 2)
  691. return -EINVAL;
  692. cfg.mask = 0x10;
  693. }), "%u.%u\n", cfg.release_major, cfg.release_minor);
  694. #undef CP2112_CONFIG_ATTR
  695. struct cp2112_pstring_attribute {
  696. struct device_attribute attr;
  697. unsigned char report;
  698. };
  699. static ssize_t pstr_store(struct device *kdev,
  700. struct device_attribute *kattr, const char *buf,
  701. size_t count)
  702. {
  703. struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
  704. struct cp2112_pstring_attribute *attr =
  705. container_of(kattr, struct cp2112_pstring_attribute, attr);
  706. struct cp2112_string_report report;
  707. int ret;
  708. memset(&report, 0, sizeof(report));
  709. ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
  710. report.string, ARRAY_SIZE(report.string));
  711. report.report = attr->report;
  712. report.length = ret * sizeof(report.string[0]) + 2;
  713. report.type = USB_DT_STRING;
  714. ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
  715. HID_FEATURE_REPORT);
  716. if (ret != report.length + 1) {
  717. hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
  718. ret);
  719. if (ret < 0)
  720. return ret;
  721. return -EIO;
  722. }
  723. chmod_sysfs_attrs(hdev);
  724. return count;
  725. }
  726. static ssize_t pstr_show(struct device *kdev,
  727. struct device_attribute *kattr, char *buf)
  728. {
  729. struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
  730. struct cp2112_pstring_attribute *attr =
  731. container_of(kattr, struct cp2112_pstring_attribute, attr);
  732. struct cp2112_string_report report;
  733. u8 length;
  734. int ret;
  735. ret = cp2112_hid_get(hdev, attr->report, &report.report,
  736. sizeof(report) - 1, HID_FEATURE_REPORT);
  737. if (ret < 3) {
  738. hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
  739. ret);
  740. if (ret < 0)
  741. return ret;
  742. return -EIO;
  743. }
  744. if (report.length < 2) {
  745. hid_err(hdev, "invalid %s string length: %d\n",
  746. kattr->attr.name, report.length);
  747. return -EIO;
  748. }
  749. length = report.length > ret - 1 ? ret - 1 : report.length;
  750. length = (length - 2) / sizeof(report.string[0]);
  751. ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
  752. PAGE_SIZE - 1);
  753. buf[ret++] = '\n';
  754. return ret;
  755. }
  756. #define CP2112_PSTR_ATTR(name, _report) \
  757. static struct cp2112_pstring_attribute dev_attr_##name = { \
  758. .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
  759. .report = _report, \
  760. };
  761. CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
  762. CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
  763. CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
  764. #undef CP2112_PSTR_ATTR
  765. static const struct attribute_group cp2112_attr_group = {
  766. .attrs = (struct attribute *[]){
  767. &dev_attr_vendor_id.attr,
  768. &dev_attr_product_id.attr,
  769. &dev_attr_max_power.attr,
  770. &dev_attr_power_mode.attr,
  771. &dev_attr_release_version.attr,
  772. &dev_attr_manufacturer.attr.attr,
  773. &dev_attr_product.attr.attr,
  774. &dev_attr_serial.attr.attr,
  775. NULL
  776. }
  777. };
  778. /* Chmoding our sysfs attributes is simply a way to expose which fields in the
  779. * PROM have already been programmed. We do not depend on this preventing
  780. * writing to these attributes since the CP2112 will simply ignore writes to
  781. * already-programmed fields. This is why there is no sense in fixing this
  782. * racy behaviour.
  783. */
  784. static void chmod_sysfs_attrs(struct hid_device *hdev)
  785. {
  786. struct attribute **attr;
  787. u8 buf[2];
  788. int ret;
  789. ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
  790. HID_FEATURE_REPORT);
  791. if (ret != sizeof(buf)) {
  792. hid_err(hdev, "error reading lock byte: %d\n", ret);
  793. return;
  794. }
  795. for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
  796. umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
  797. ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
  798. if (ret < 0)
  799. hid_err(hdev, "error chmoding sysfs file %s\n",
  800. (*attr)->name);
  801. buf[1] >>= 1;
  802. }
  803. }
  804. static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
  805. {
  806. struct cp2112_device *dev;
  807. u8 buf[3];
  808. struct cp2112_smbus_config_report config;
  809. int ret;
  810. ret = hid_parse(hdev);
  811. if (ret) {
  812. hid_err(hdev, "parse failed\n");
  813. return ret;
  814. }
  815. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  816. if (ret) {
  817. hid_err(hdev, "hw start failed\n");
  818. return ret;
  819. }
  820. ret = hid_hw_open(hdev);
  821. if (ret) {
  822. hid_err(hdev, "hw open failed\n");
  823. goto err_hid_stop;
  824. }
  825. ret = hid_hw_power(hdev, PM_HINT_FULLON);
  826. if (ret < 0) {
  827. hid_err(hdev, "power management error: %d\n", ret);
  828. goto err_hid_close;
  829. }
  830. ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
  831. HID_FEATURE_REPORT);
  832. if (ret != sizeof(buf)) {
  833. hid_err(hdev, "error requesting version\n");
  834. if (ret >= 0)
  835. ret = -EIO;
  836. goto err_power_normal;
  837. }
  838. hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
  839. buf[1], buf[2]);
  840. ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
  841. sizeof(config), HID_FEATURE_REPORT);
  842. if (ret != sizeof(config)) {
  843. hid_err(hdev, "error requesting SMBus config\n");
  844. if (ret >= 0)
  845. ret = -EIO;
  846. goto err_power_normal;
  847. }
  848. config.retry_time = cpu_to_be16(1);
  849. ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
  850. HID_FEATURE_REPORT);
  851. if (ret != sizeof(config)) {
  852. hid_err(hdev, "error setting SMBus config\n");
  853. if (ret >= 0)
  854. ret = -EIO;
  855. goto err_power_normal;
  856. }
  857. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  858. if (!dev) {
  859. ret = -ENOMEM;
  860. goto err_power_normal;
  861. }
  862. hid_set_drvdata(hdev, (void *)dev);
  863. dev->hdev = hdev;
  864. dev->adap.owner = THIS_MODULE;
  865. dev->adap.class = I2C_CLASS_HWMON;
  866. dev->adap.algo = &smbus_algorithm;
  867. dev->adap.algo_data = dev;
  868. dev->adap.dev.parent = &hdev->dev;
  869. snprintf(dev->adap.name, sizeof(dev->adap.name),
  870. "CP2112 SMBus Bridge on hiddev%d", hdev->minor);
  871. init_waitqueue_head(&dev->wait);
  872. hid_device_io_start(hdev);
  873. ret = i2c_add_adapter(&dev->adap);
  874. hid_device_io_stop(hdev);
  875. if (ret) {
  876. hid_err(hdev, "error registering i2c adapter\n");
  877. goto err_free_dev;
  878. }
  879. hid_dbg(hdev, "adapter registered\n");
  880. dev->gc.label = "cp2112_gpio";
  881. dev->gc.direction_input = cp2112_gpio_direction_input;
  882. dev->gc.direction_output = cp2112_gpio_direction_output;
  883. dev->gc.set = cp2112_gpio_set;
  884. dev->gc.get = cp2112_gpio_get;
  885. dev->gc.base = -1;
  886. dev->gc.ngpio = 8;
  887. dev->gc.can_sleep = 1;
  888. dev->gc.dev = &hdev->dev;
  889. ret = gpiochip_add(&dev->gc);
  890. if (ret < 0) {
  891. hid_err(hdev, "error registering gpio chip\n");
  892. goto err_free_i2c;
  893. }
  894. ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
  895. if (ret < 0) {
  896. hid_err(hdev, "error creating sysfs attrs\n");
  897. goto err_gpiochip_remove;
  898. }
  899. chmod_sysfs_attrs(hdev);
  900. hid_hw_power(hdev, PM_HINT_NORMAL);
  901. return ret;
  902. err_gpiochip_remove:
  903. gpiochip_remove(&dev->gc);
  904. err_free_i2c:
  905. i2c_del_adapter(&dev->adap);
  906. err_free_dev:
  907. kfree(dev);
  908. err_power_normal:
  909. hid_hw_power(hdev, PM_HINT_NORMAL);
  910. err_hid_close:
  911. hid_hw_close(hdev);
  912. err_hid_stop:
  913. hid_hw_stop(hdev);
  914. return ret;
  915. }
  916. static void cp2112_remove(struct hid_device *hdev)
  917. {
  918. struct cp2112_device *dev = hid_get_drvdata(hdev);
  919. sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
  920. gpiochip_remove(&dev->gc);
  921. i2c_del_adapter(&dev->adap);
  922. /* i2c_del_adapter has finished removing all i2c devices from our
  923. * adapter. Well behaved devices should no longer call our cp2112_xfer
  924. * and should have waited for any pending calls to finish. It has also
  925. * waited for device_unregister(&adap->dev) to complete. Therefore we
  926. * can safely free our struct cp2112_device.
  927. */
  928. hid_hw_close(hdev);
  929. hid_hw_stop(hdev);
  930. kfree(dev);
  931. }
  932. static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
  933. u8 *data, int size)
  934. {
  935. struct cp2112_device *dev = hid_get_drvdata(hdev);
  936. struct cp2112_xfer_status_report *xfer = (void *)data;
  937. switch (data[0]) {
  938. case CP2112_TRANSFER_STATUS_RESPONSE:
  939. hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
  940. xfer->status0, xfer->status1,
  941. be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
  942. switch (xfer->status0) {
  943. case STATUS0_IDLE:
  944. dev->xfer_status = -EAGAIN;
  945. break;
  946. case STATUS0_BUSY:
  947. dev->xfer_status = -EBUSY;
  948. break;
  949. case STATUS0_COMPLETE:
  950. dev->xfer_status = be16_to_cpu(xfer->length);
  951. break;
  952. case STATUS0_ERROR:
  953. switch (xfer->status1) {
  954. case STATUS1_TIMEOUT_NACK:
  955. case STATUS1_TIMEOUT_BUS:
  956. dev->xfer_status = -ETIMEDOUT;
  957. break;
  958. default:
  959. dev->xfer_status = -EIO;
  960. break;
  961. }
  962. break;
  963. default:
  964. dev->xfer_status = -EINVAL;
  965. break;
  966. }
  967. atomic_set(&dev->xfer_avail, 1);
  968. break;
  969. case CP2112_DATA_READ_RESPONSE:
  970. hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
  971. dev->read_length = data[2];
  972. if (dev->read_length > sizeof(dev->read_data))
  973. dev->read_length = sizeof(dev->read_data);
  974. memcpy(dev->read_data, &data[3], dev->read_length);
  975. atomic_set(&dev->read_avail, 1);
  976. break;
  977. default:
  978. hid_err(hdev, "unknown report\n");
  979. return 0;
  980. }
  981. wake_up_interruptible(&dev->wait);
  982. return 1;
  983. }
  984. static struct hid_driver cp2112_driver = {
  985. .name = "cp2112",
  986. .id_table = cp2112_devices,
  987. .probe = cp2112_probe,
  988. .remove = cp2112_remove,
  989. .raw_event = cp2112_raw_event,
  990. };
  991. module_hid_driver(cp2112_driver);
  992. MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
  993. MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
  994. MODULE_LICENSE("GPL");