hid-cp2112.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. cp2112_gpio_set(chip, offset, value);
  213. ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
  214. sizeof(buf), HID_FEATURE_REPORT,
  215. HID_REQ_GET_REPORT);
  216. if (ret != sizeof(buf)) {
  217. hid_err(hdev, "error requesting GPIO config: %d\n", ret);
  218. return ret;
  219. }
  220. buf[1] |= 1 << offset;
  221. buf[2] = gpio_push_pull;
  222. ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
  223. HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
  224. if (ret < 0) {
  225. hid_err(hdev, "error setting GPIO config: %d\n", ret);
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
  231. u8 *data, size_t count, unsigned char report_type)
  232. {
  233. u8 *buf;
  234. int ret;
  235. buf = kmalloc(count, GFP_KERNEL);
  236. if (!buf)
  237. return -ENOMEM;
  238. ret = hid_hw_raw_request(hdev, report_number, buf, count,
  239. report_type, HID_REQ_GET_REPORT);
  240. memcpy(data, buf, count);
  241. kfree(buf);
  242. return ret;
  243. }
  244. static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
  245. unsigned char report_type)
  246. {
  247. u8 *buf;
  248. int ret;
  249. buf = kmemdup(data, count, GFP_KERNEL);
  250. if (!buf)
  251. return -ENOMEM;
  252. if (report_type == HID_OUTPUT_REPORT)
  253. ret = hid_hw_output_report(hdev, buf, count);
  254. else
  255. ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
  256. HID_REQ_SET_REPORT);
  257. kfree(buf);
  258. return ret;
  259. }
  260. static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
  261. {
  262. int ret = 0;
  263. /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
  264. * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
  265. * come in cp2112_raw_event or timeout. There will only be one of these
  266. * in flight at any one time. The timeout is extremely large and is a
  267. * last resort if the CP2112 has died. If we do timeout we don't expect
  268. * to receive the response which would cause data races, it's not like
  269. * we can do anything about it anyway.
  270. */
  271. ret = wait_event_interruptible_timeout(dev->wait,
  272. atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
  273. if (-ERESTARTSYS == ret)
  274. return ret;
  275. if (!ret)
  276. return -ETIMEDOUT;
  277. atomic_set(avail, 0);
  278. return 0;
  279. }
  280. static int cp2112_xfer_status(struct cp2112_device *dev)
  281. {
  282. struct hid_device *hdev = dev->hdev;
  283. u8 buf[2];
  284. int ret;
  285. buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
  286. buf[1] = 0x01;
  287. atomic_set(&dev->xfer_avail, 0);
  288. ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
  289. if (ret < 0) {
  290. hid_warn(hdev, "Error requesting status: %d\n", ret);
  291. return ret;
  292. }
  293. ret = cp2112_wait(dev, &dev->xfer_avail);
  294. if (ret)
  295. return ret;
  296. return dev->xfer_status;
  297. }
  298. static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
  299. {
  300. struct hid_device *hdev = dev->hdev;
  301. struct cp2112_force_read_report report;
  302. int ret;
  303. report.report = CP2112_DATA_READ_FORCE_SEND;
  304. report.length = cpu_to_be16(size);
  305. atomic_set(&dev->read_avail, 0);
  306. ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
  307. HID_OUTPUT_REPORT);
  308. if (ret < 0) {
  309. hid_warn(hdev, "Error requesting data: %d\n", ret);
  310. return ret;
  311. }
  312. ret = cp2112_wait(dev, &dev->read_avail);
  313. if (ret)
  314. return ret;
  315. hid_dbg(hdev, "read %d of %zd bytes requested\n",
  316. dev->read_length, size);
  317. if (size > dev->read_length)
  318. size = dev->read_length;
  319. memcpy(data, dev->read_data, size);
  320. return dev->read_length;
  321. }
  322. static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
  323. {
  324. struct cp2112_read_req_report *report = buf;
  325. if (length < 1 || length > 512)
  326. return -EINVAL;
  327. report->report = CP2112_DATA_READ_REQUEST;
  328. report->slave_address = slave_address << 1;
  329. report->length = cpu_to_be16(length);
  330. return sizeof(*report);
  331. }
  332. static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
  333. u8 command, u8 *data, u8 data_length)
  334. {
  335. struct cp2112_write_read_req_report *report = buf;
  336. if (length < 1 || length > 512
  337. || data_length > sizeof(report->target_address) - 1)
  338. return -EINVAL;
  339. report->report = CP2112_DATA_WRITE_READ_REQUEST;
  340. report->slave_address = slave_address << 1;
  341. report->length = cpu_to_be16(length);
  342. report->target_address_length = data_length + 1;
  343. report->target_address[0] = command;
  344. memcpy(&report->target_address[1], data, data_length);
  345. return data_length + 6;
  346. }
  347. static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
  348. u8 data_length)
  349. {
  350. struct cp2112_write_req_report *report = buf;
  351. if (data_length > sizeof(report->data) - 1)
  352. return -EINVAL;
  353. report->report = CP2112_DATA_WRITE_REQUEST;
  354. report->slave_address = slave_address << 1;
  355. report->length = data_length + 1;
  356. report->data[0] = command;
  357. memcpy(&report->data[1], data, data_length);
  358. return data_length + 4;
  359. }
  360. static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
  361. unsigned short flags, char read_write, u8 command,
  362. int size, union i2c_smbus_data *data)
  363. {
  364. struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
  365. struct hid_device *hdev = dev->hdev;
  366. u8 buf[64];
  367. __be16 word;
  368. ssize_t count;
  369. size_t read_length = 0;
  370. unsigned int retries;
  371. int ret;
  372. hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
  373. read_write == I2C_SMBUS_WRITE ? "write" : "read",
  374. addr, flags, command, size);
  375. switch (size) {
  376. case I2C_SMBUS_BYTE:
  377. read_length = 1;
  378. if (I2C_SMBUS_READ == read_write)
  379. count = cp2112_read_req(buf, addr, read_length);
  380. else
  381. count = cp2112_write_req(buf, addr, data->byte, NULL,
  382. 0);
  383. break;
  384. case I2C_SMBUS_BYTE_DATA:
  385. read_length = 1;
  386. if (I2C_SMBUS_READ == read_write)
  387. count = cp2112_write_read_req(buf, addr, read_length,
  388. command, NULL, 0);
  389. else
  390. count = cp2112_write_req(buf, addr, command,
  391. &data->byte, 1);
  392. break;
  393. case I2C_SMBUS_WORD_DATA:
  394. read_length = 2;
  395. word = cpu_to_be16(data->word);
  396. if (I2C_SMBUS_READ == read_write)
  397. count = cp2112_write_read_req(buf, addr, read_length,
  398. command, NULL, 0);
  399. else
  400. count = cp2112_write_req(buf, addr, command,
  401. (u8 *)&word, 2);
  402. break;
  403. case I2C_SMBUS_PROC_CALL:
  404. size = I2C_SMBUS_WORD_DATA;
  405. read_write = I2C_SMBUS_READ;
  406. read_length = 2;
  407. word = cpu_to_be16(data->word);
  408. count = cp2112_write_read_req(buf, addr, read_length, command,
  409. (u8 *)&word, 2);
  410. break;
  411. case I2C_SMBUS_I2C_BLOCK_DATA:
  412. size = I2C_SMBUS_BLOCK_DATA;
  413. /* fallthrough */
  414. case I2C_SMBUS_BLOCK_DATA:
  415. if (I2C_SMBUS_READ == read_write) {
  416. count = cp2112_write_read_req(buf, addr,
  417. I2C_SMBUS_BLOCK_MAX,
  418. command, NULL, 0);
  419. } else {
  420. count = cp2112_write_req(buf, addr, command,
  421. data->block,
  422. data->block[0] + 1);
  423. }
  424. break;
  425. case I2C_SMBUS_BLOCK_PROC_CALL:
  426. size = I2C_SMBUS_BLOCK_DATA;
  427. read_write = I2C_SMBUS_READ;
  428. count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
  429. command, data->block,
  430. data->block[0] + 1);
  431. break;
  432. default:
  433. hid_warn(hdev, "Unsupported transaction %d\n", size);
  434. return -EOPNOTSUPP;
  435. }
  436. if (count < 0)
  437. return count;
  438. ret = hid_hw_power(hdev, PM_HINT_FULLON);
  439. if (ret < 0) {
  440. hid_err(hdev, "power management error: %d\n", ret);
  441. return ret;
  442. }
  443. ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
  444. if (ret < 0) {
  445. hid_warn(hdev, "Error starting transaction: %d\n", ret);
  446. goto power_normal;
  447. }
  448. for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
  449. ret = cp2112_xfer_status(dev);
  450. if (-EBUSY == ret)
  451. continue;
  452. if (ret < 0)
  453. goto power_normal;
  454. break;
  455. }
  456. if (XFER_STATUS_RETRIES <= retries) {
  457. hid_warn(hdev, "Transfer timed out, cancelling.\n");
  458. buf[0] = CP2112_CANCEL_TRANSFER;
  459. buf[1] = 0x01;
  460. ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
  461. if (ret < 0)
  462. hid_warn(hdev, "Error cancelling transaction: %d\n",
  463. ret);
  464. ret = -ETIMEDOUT;
  465. goto power_normal;
  466. }
  467. if (I2C_SMBUS_WRITE == read_write) {
  468. ret = 0;
  469. goto power_normal;
  470. }
  471. if (I2C_SMBUS_BLOCK_DATA == size)
  472. read_length = ret;
  473. ret = cp2112_read(dev, buf, read_length);
  474. if (ret < 0)
  475. goto power_normal;
  476. if (ret != read_length) {
  477. hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
  478. ret = -EIO;
  479. goto power_normal;
  480. }
  481. switch (size) {
  482. case I2C_SMBUS_BYTE:
  483. case I2C_SMBUS_BYTE_DATA:
  484. data->byte = buf[0];
  485. break;
  486. case I2C_SMBUS_WORD_DATA:
  487. data->word = be16_to_cpup((__be16 *)buf);
  488. break;
  489. case I2C_SMBUS_BLOCK_DATA:
  490. if (read_length > I2C_SMBUS_BLOCK_MAX) {
  491. ret = -EPROTO;
  492. goto power_normal;
  493. }
  494. memcpy(data->block, buf, read_length);
  495. break;
  496. }
  497. ret = 0;
  498. power_normal:
  499. hid_hw_power(hdev, PM_HINT_NORMAL);
  500. hid_dbg(hdev, "transfer finished: %d\n", ret);
  501. return ret;
  502. }
  503. static u32 cp2112_functionality(struct i2c_adapter *adap)
  504. {
  505. return I2C_FUNC_SMBUS_BYTE |
  506. I2C_FUNC_SMBUS_BYTE_DATA |
  507. I2C_FUNC_SMBUS_WORD_DATA |
  508. I2C_FUNC_SMBUS_BLOCK_DATA |
  509. I2C_FUNC_SMBUS_I2C_BLOCK |
  510. I2C_FUNC_SMBUS_PROC_CALL |
  511. I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
  512. }
  513. static const struct i2c_algorithm smbus_algorithm = {
  514. .smbus_xfer = cp2112_xfer,
  515. .functionality = cp2112_functionality,
  516. };
  517. static int cp2112_get_usb_config(struct hid_device *hdev,
  518. struct cp2112_usb_config_report *cfg)
  519. {
  520. int ret;
  521. ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
  522. HID_FEATURE_REPORT);
  523. if (ret != sizeof(*cfg)) {
  524. hid_err(hdev, "error reading usb config: %d\n", ret);
  525. if (ret < 0)
  526. return ret;
  527. return -EIO;
  528. }
  529. return 0;
  530. }
  531. static int cp2112_set_usb_config(struct hid_device *hdev,
  532. struct cp2112_usb_config_report *cfg)
  533. {
  534. int ret;
  535. BUG_ON(cfg->report != CP2112_USB_CONFIG);
  536. ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
  537. HID_FEATURE_REPORT);
  538. if (ret != sizeof(*cfg)) {
  539. hid_err(hdev, "error writing usb config: %d\n", ret);
  540. if (ret < 0)
  541. return ret;
  542. return -EIO;
  543. }
  544. return 0;
  545. }
  546. static void chmod_sysfs_attrs(struct hid_device *hdev);
  547. #define CP2112_CONFIG_ATTR(name, store, format, ...) \
  548. static ssize_t name##_store(struct device *kdev, \
  549. struct device_attribute *attr, const char *buf, \
  550. size_t count) \
  551. { \
  552. struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
  553. struct cp2112_usb_config_report cfg; \
  554. int ret = cp2112_get_usb_config(hdev, &cfg); \
  555. if (ret) \
  556. return ret; \
  557. store; \
  558. ret = cp2112_set_usb_config(hdev, &cfg); \
  559. if (ret) \
  560. return ret; \
  561. chmod_sysfs_attrs(hdev); \
  562. return count; \
  563. } \
  564. static ssize_t name##_show(struct device *kdev, \
  565. struct device_attribute *attr, char *buf) \
  566. { \
  567. struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
  568. struct cp2112_usb_config_report cfg; \
  569. int ret = cp2112_get_usb_config(hdev, &cfg); \
  570. if (ret) \
  571. return ret; \
  572. return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
  573. } \
  574. static DEVICE_ATTR_RW(name);
  575. CP2112_CONFIG_ATTR(vendor_id, ({
  576. u16 vid;
  577. if (sscanf(buf, "%hi", &vid) != 1)
  578. return -EINVAL;
  579. cfg.vid = cpu_to_le16(vid);
  580. cfg.mask = 0x01;
  581. }), "0x%04x\n", le16_to_cpu(cfg.vid));
  582. CP2112_CONFIG_ATTR(product_id, ({
  583. u16 pid;
  584. if (sscanf(buf, "%hi", &pid) != 1)
  585. return -EINVAL;
  586. cfg.pid = cpu_to_le16(pid);
  587. cfg.mask = 0x02;
  588. }), "0x%04x\n", le16_to_cpu(cfg.pid));
  589. CP2112_CONFIG_ATTR(max_power, ({
  590. int mA;
  591. if (sscanf(buf, "%i", &mA) != 1)
  592. return -EINVAL;
  593. cfg.max_power = (mA + 1) / 2;
  594. cfg.mask = 0x04;
  595. }), "%u mA\n", cfg.max_power * 2);
  596. CP2112_CONFIG_ATTR(power_mode, ({
  597. if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
  598. return -EINVAL;
  599. cfg.mask = 0x08;
  600. }), "%u\n", cfg.power_mode);
  601. CP2112_CONFIG_ATTR(release_version, ({
  602. if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
  603. != 2)
  604. return -EINVAL;
  605. cfg.mask = 0x10;
  606. }), "%u.%u\n", cfg.release_major, cfg.release_minor);
  607. #undef CP2112_CONFIG_ATTR
  608. struct cp2112_pstring_attribute {
  609. struct device_attribute attr;
  610. unsigned char report;
  611. };
  612. static ssize_t pstr_store(struct device *kdev,
  613. struct device_attribute *kattr, const char *buf,
  614. size_t count)
  615. {
  616. struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
  617. struct cp2112_pstring_attribute *attr =
  618. container_of(kattr, struct cp2112_pstring_attribute, attr);
  619. struct cp2112_string_report report;
  620. int ret;
  621. memset(&report, 0, sizeof(report));
  622. ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
  623. report.string, ARRAY_SIZE(report.string));
  624. report.report = attr->report;
  625. report.length = ret * sizeof(report.string[0]) + 2;
  626. report.type = USB_DT_STRING;
  627. ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
  628. HID_FEATURE_REPORT);
  629. if (ret != report.length + 1) {
  630. hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
  631. ret);
  632. if (ret < 0)
  633. return ret;
  634. return -EIO;
  635. }
  636. chmod_sysfs_attrs(hdev);
  637. return count;
  638. }
  639. static ssize_t pstr_show(struct device *kdev,
  640. struct device_attribute *kattr, char *buf)
  641. {
  642. struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
  643. struct cp2112_pstring_attribute *attr =
  644. container_of(kattr, struct cp2112_pstring_attribute, attr);
  645. struct cp2112_string_report report;
  646. u8 length;
  647. int ret;
  648. ret = cp2112_hid_get(hdev, attr->report, &report.report,
  649. sizeof(report) - 1, HID_FEATURE_REPORT);
  650. if (ret < 3) {
  651. hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
  652. ret);
  653. if (ret < 0)
  654. return ret;
  655. return -EIO;
  656. }
  657. if (report.length < 2) {
  658. hid_err(hdev, "invalid %s string length: %d\n",
  659. kattr->attr.name, report.length);
  660. return -EIO;
  661. }
  662. length = report.length > ret - 1 ? ret - 1 : report.length;
  663. length = (length - 2) / sizeof(report.string[0]);
  664. ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
  665. PAGE_SIZE - 1);
  666. buf[ret++] = '\n';
  667. return ret;
  668. }
  669. #define CP2112_PSTR_ATTR(name, _report) \
  670. static struct cp2112_pstring_attribute dev_attr_##name = { \
  671. .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
  672. .report = _report, \
  673. };
  674. CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
  675. CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
  676. CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
  677. #undef CP2112_PSTR_ATTR
  678. static const struct attribute_group cp2112_attr_group = {
  679. .attrs = (struct attribute *[]){
  680. &dev_attr_vendor_id.attr,
  681. &dev_attr_product_id.attr,
  682. &dev_attr_max_power.attr,
  683. &dev_attr_power_mode.attr,
  684. &dev_attr_release_version.attr,
  685. &dev_attr_manufacturer.attr.attr,
  686. &dev_attr_product.attr.attr,
  687. &dev_attr_serial.attr.attr,
  688. NULL
  689. }
  690. };
  691. /* Chmoding our sysfs attributes is simply a way to expose which fields in the
  692. * PROM have already been programmed. We do not depend on this preventing
  693. * writing to these attributes since the CP2112 will simply ignore writes to
  694. * already-programmed fields. This is why there is no sense in fixing this
  695. * racy behaviour.
  696. */
  697. static void chmod_sysfs_attrs(struct hid_device *hdev)
  698. {
  699. struct attribute **attr;
  700. u8 buf[2];
  701. int ret;
  702. ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
  703. HID_FEATURE_REPORT);
  704. if (ret != sizeof(buf)) {
  705. hid_err(hdev, "error reading lock byte: %d\n", ret);
  706. return;
  707. }
  708. for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
  709. umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
  710. ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
  711. if (ret < 0)
  712. hid_err(hdev, "error chmoding sysfs file %s\n",
  713. (*attr)->name);
  714. buf[1] >>= 1;
  715. }
  716. }
  717. static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
  718. {
  719. struct cp2112_device *dev;
  720. u8 buf[3];
  721. struct cp2112_smbus_config_report config;
  722. int ret;
  723. ret = hid_parse(hdev);
  724. if (ret) {
  725. hid_err(hdev, "parse failed\n");
  726. return ret;
  727. }
  728. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  729. if (ret) {
  730. hid_err(hdev, "hw start failed\n");
  731. return ret;
  732. }
  733. ret = hid_hw_open(hdev);
  734. if (ret) {
  735. hid_err(hdev, "hw open failed\n");
  736. goto err_hid_stop;
  737. }
  738. ret = hid_hw_power(hdev, PM_HINT_FULLON);
  739. if (ret < 0) {
  740. hid_err(hdev, "power management error: %d\n", ret);
  741. goto err_hid_close;
  742. }
  743. ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
  744. HID_FEATURE_REPORT);
  745. if (ret != sizeof(buf)) {
  746. hid_err(hdev, "error requesting version\n");
  747. if (ret >= 0)
  748. ret = -EIO;
  749. goto err_power_normal;
  750. }
  751. hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
  752. buf[1], buf[2]);
  753. ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
  754. sizeof(config), HID_FEATURE_REPORT);
  755. if (ret != sizeof(config)) {
  756. hid_err(hdev, "error requesting SMBus config\n");
  757. if (ret >= 0)
  758. ret = -EIO;
  759. goto err_power_normal;
  760. }
  761. config.retry_time = cpu_to_be16(1);
  762. ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
  763. HID_FEATURE_REPORT);
  764. if (ret != sizeof(config)) {
  765. hid_err(hdev, "error setting SMBus config\n");
  766. if (ret >= 0)
  767. ret = -EIO;
  768. goto err_power_normal;
  769. }
  770. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  771. if (!dev) {
  772. ret = -ENOMEM;
  773. goto err_power_normal;
  774. }
  775. hid_set_drvdata(hdev, (void *)dev);
  776. dev->hdev = hdev;
  777. dev->adap.owner = THIS_MODULE;
  778. dev->adap.class = I2C_CLASS_HWMON;
  779. dev->adap.algo = &smbus_algorithm;
  780. dev->adap.algo_data = dev;
  781. dev->adap.dev.parent = &hdev->dev;
  782. snprintf(dev->adap.name, sizeof(dev->adap.name),
  783. "CP2112 SMBus Bridge on hiddev%d", hdev->minor);
  784. init_waitqueue_head(&dev->wait);
  785. hid_device_io_start(hdev);
  786. ret = i2c_add_adapter(&dev->adap);
  787. hid_device_io_stop(hdev);
  788. if (ret) {
  789. hid_err(hdev, "error registering i2c adapter\n");
  790. goto err_free_dev;
  791. }
  792. hid_dbg(hdev, "adapter registered\n");
  793. dev->gc.label = "cp2112_gpio";
  794. dev->gc.direction_input = cp2112_gpio_direction_input;
  795. dev->gc.direction_output = cp2112_gpio_direction_output;
  796. dev->gc.set = cp2112_gpio_set;
  797. dev->gc.get = cp2112_gpio_get;
  798. dev->gc.base = -1;
  799. dev->gc.ngpio = 8;
  800. dev->gc.can_sleep = 1;
  801. dev->gc.dev = &hdev->dev;
  802. ret = gpiochip_add(&dev->gc);
  803. if (ret < 0) {
  804. hid_err(hdev, "error registering gpio chip\n");
  805. goto err_free_i2c;
  806. }
  807. ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
  808. if (ret < 0) {
  809. hid_err(hdev, "error creating sysfs attrs\n");
  810. goto err_gpiochip_remove;
  811. }
  812. chmod_sysfs_attrs(hdev);
  813. hid_hw_power(hdev, PM_HINT_NORMAL);
  814. return ret;
  815. err_gpiochip_remove:
  816. if (gpiochip_remove(&dev->gc) < 0)
  817. hid_err(hdev, "error removing gpio chip\n");
  818. err_free_i2c:
  819. i2c_del_adapter(&dev->adap);
  820. err_free_dev:
  821. kfree(dev);
  822. err_power_normal:
  823. hid_hw_power(hdev, PM_HINT_NORMAL);
  824. err_hid_close:
  825. hid_hw_close(hdev);
  826. err_hid_stop:
  827. hid_hw_stop(hdev);
  828. return ret;
  829. }
  830. static void cp2112_remove(struct hid_device *hdev)
  831. {
  832. struct cp2112_device *dev = hid_get_drvdata(hdev);
  833. sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
  834. if (gpiochip_remove(&dev->gc))
  835. hid_err(hdev, "unable to remove gpio chip\n");
  836. i2c_del_adapter(&dev->adap);
  837. /* i2c_del_adapter has finished removing all i2c devices from our
  838. * adapter. Well behaved devices should no longer call our cp2112_xfer
  839. * and should have waited for any pending calls to finish. It has also
  840. * waited for device_unregister(&adap->dev) to complete. Therefore we
  841. * can safely free our struct cp2112_device.
  842. */
  843. hid_hw_close(hdev);
  844. hid_hw_stop(hdev);
  845. kfree(dev);
  846. }
  847. static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
  848. u8 *data, int size)
  849. {
  850. struct cp2112_device *dev = hid_get_drvdata(hdev);
  851. struct cp2112_xfer_status_report *xfer = (void *)data;
  852. switch (data[0]) {
  853. case CP2112_TRANSFER_STATUS_RESPONSE:
  854. hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
  855. xfer->status0, xfer->status1,
  856. be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
  857. switch (xfer->status0) {
  858. case STATUS0_IDLE:
  859. dev->xfer_status = -EAGAIN;
  860. break;
  861. case STATUS0_BUSY:
  862. dev->xfer_status = -EBUSY;
  863. break;
  864. case STATUS0_COMPLETE:
  865. dev->xfer_status = be16_to_cpu(xfer->length);
  866. break;
  867. case STATUS0_ERROR:
  868. switch (xfer->status1) {
  869. case STATUS1_TIMEOUT_NACK:
  870. case STATUS1_TIMEOUT_BUS:
  871. dev->xfer_status = -ETIMEDOUT;
  872. break;
  873. default:
  874. dev->xfer_status = -EIO;
  875. break;
  876. }
  877. break;
  878. default:
  879. dev->xfer_status = -EINVAL;
  880. break;
  881. }
  882. atomic_set(&dev->xfer_avail, 1);
  883. break;
  884. case CP2112_DATA_READ_RESPONSE:
  885. hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
  886. dev->read_length = data[2];
  887. if (dev->read_length > sizeof(dev->read_data))
  888. dev->read_length = sizeof(dev->read_data);
  889. memcpy(dev->read_data, &data[3], dev->read_length);
  890. atomic_set(&dev->read_avail, 1);
  891. break;
  892. default:
  893. hid_err(hdev, "unknown report\n");
  894. return 0;
  895. }
  896. wake_up_interruptible(&dev->wait);
  897. return 1;
  898. }
  899. static struct hid_driver cp2112_driver = {
  900. .name = "cp2112",
  901. .id_table = cp2112_devices,
  902. .probe = cp2112_probe,
  903. .remove = cp2112_remove,
  904. .raw_event = cp2112_raw_event,
  905. };
  906. module_hid_driver(cp2112_driver);
  907. MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
  908. MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
  909. MODULE_LICENSE("GPL");