hid-cp2112.c 32 KB

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