hid-cp2112.c 32 KB

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