usb-ljca.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel La Jolla Cove Adapter USB driver
  4. *
  5. * Copyright (c) 2023, Intel Corporation.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/auxiliary_bus.h>
  9. #include <linux/dev_printk.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/types.h>
  17. #include <linux/usb.h>
  18. #include <linux/usb/ljca.h>
  19. #include <linux/unaligned.h>
  20. /* command flags */
  21. #define LJCA_ACK_FLAG BIT(0)
  22. #define LJCA_RESP_FLAG BIT(1)
  23. #define LJCA_CMPL_FLAG BIT(2)
  24. #define LJCA_MAX_PACKET_SIZE 64u
  25. #define LJCA_MAX_PAYLOAD_SIZE \
  26. (LJCA_MAX_PACKET_SIZE - sizeof(struct ljca_msg))
  27. #define LJCA_WRITE_TIMEOUT_MS 200
  28. #define LJCA_WRITE_ACK_TIMEOUT_MS 500
  29. #define LJCA_ENUM_CLIENT_TIMEOUT_MS 20
  30. /* ljca client type */
  31. enum ljca_client_type {
  32. LJCA_CLIENT_MNG = 1,
  33. LJCA_CLIENT_GPIO = 3,
  34. LJCA_CLIENT_I2C = 4,
  35. LJCA_CLIENT_SPI = 5,
  36. };
  37. /* MNG client commands */
  38. enum ljca_mng_cmd {
  39. LJCA_MNG_RESET = 2,
  40. LJCA_MNG_ENUM_GPIO = 4,
  41. LJCA_MNG_ENUM_I2C = 5,
  42. LJCA_MNG_ENUM_SPI = 8,
  43. };
  44. /* ljca client acpi _ADR */
  45. enum ljca_client_acpi_adr {
  46. LJCA_GPIO_ACPI_ADR,
  47. LJCA_I2C1_ACPI_ADR,
  48. LJCA_I2C2_ACPI_ADR,
  49. LJCA_SPI1_ACPI_ADR,
  50. LJCA_SPI2_ACPI_ADR,
  51. LJCA_CLIENT_ACPI_ADR_MAX,
  52. };
  53. /* ljca cmd message structure */
  54. struct ljca_msg {
  55. u8 type;
  56. u8 cmd;
  57. u8 flags;
  58. u8 len;
  59. u8 data[] __counted_by(len);
  60. } __packed;
  61. struct ljca_i2c_ctr_info {
  62. u8 id;
  63. u8 capacity;
  64. u8 intr_pin;
  65. } __packed;
  66. struct ljca_i2c_descriptor {
  67. u8 num;
  68. struct ljca_i2c_ctr_info info[] __counted_by(num);
  69. } __packed;
  70. struct ljca_spi_ctr_info {
  71. u8 id;
  72. u8 capacity;
  73. u8 intr_pin;
  74. } __packed;
  75. struct ljca_spi_descriptor {
  76. u8 num;
  77. struct ljca_spi_ctr_info info[] __counted_by(num);
  78. } __packed;
  79. struct ljca_bank_descriptor {
  80. u8 bank_id;
  81. u8 pin_num;
  82. /* 1 bit for each gpio, 1 means valid */
  83. __le32 valid_pins;
  84. } __packed;
  85. struct ljca_gpio_descriptor {
  86. u8 pins_per_bank;
  87. u8 bank_num;
  88. struct ljca_bank_descriptor bank_desc[] __counted_by(bank_num);
  89. } __packed;
  90. /**
  91. * struct ljca_adapter - represent a ljca adapter
  92. *
  93. * @intf: the usb interface for this ljca adapter
  94. * @usb_dev: the usb device for this ljca adapter
  95. * @dev: the specific device info of the usb interface
  96. * @rx_pipe: bulk in pipe for receive data from firmware
  97. * @tx_pipe: bulk out pipe for send data to firmware
  98. * @rx_urb: urb used for the bulk in pipe
  99. * @rx_buf: buffer used to receive command response and event
  100. * @rx_len: length of rx buffer
  101. * @ex_buf: external buffer to save command response
  102. * @ex_buf_len: length of external buffer
  103. * @actual_length: actual length of data copied to external buffer
  104. * @tx_buf: buffer used to download command to firmware
  105. * @tx_buf_len: length of tx buffer
  106. * @lock: spinlock to protect tx_buf and ex_buf
  107. * @cmd_completion: completion object as the command receives ack
  108. * @mutex: mutex to avoid command download concurrently
  109. * @client_list: client device list
  110. * @disconnect: usb disconnect ongoing or not
  111. * @reset_id: used to reset firmware
  112. */
  113. struct ljca_adapter {
  114. struct usb_interface *intf;
  115. struct usb_device *usb_dev;
  116. struct device *dev;
  117. unsigned int rx_pipe;
  118. unsigned int tx_pipe;
  119. struct urb *rx_urb;
  120. void *rx_buf;
  121. unsigned int rx_len;
  122. u8 *ex_buf;
  123. u8 ex_buf_len;
  124. u8 actual_length;
  125. void *tx_buf;
  126. u8 tx_buf_len;
  127. spinlock_t lock;
  128. struct completion cmd_completion;
  129. struct mutex mutex;
  130. struct list_head client_list;
  131. bool disconnect;
  132. u32 reset_id;
  133. };
  134. struct ljca_match_ids_walk_data {
  135. const struct acpi_device_id *ids;
  136. const char *uid;
  137. struct acpi_device *adev;
  138. };
  139. static const struct acpi_device_id ljca_gpio_hids[] = {
  140. { "INTC1074" },
  141. { "INTC1096" },
  142. { "INTC100B" },
  143. { "INTC10D1" },
  144. { "INTC10B5" },
  145. {},
  146. };
  147. static const struct acpi_device_id ljca_i2c_hids[] = {
  148. { "INTC1075" },
  149. { "INTC1097" },
  150. { "INTC100C" },
  151. { "INTC10D2" },
  152. {},
  153. };
  154. static const struct acpi_device_id ljca_spi_hids[] = {
  155. { "INTC1091" },
  156. { "INTC1098" },
  157. { "INTC100D" },
  158. { "INTC10D3" },
  159. {},
  160. };
  161. static void ljca_handle_event(struct ljca_adapter *adap,
  162. struct ljca_msg *header)
  163. {
  164. struct ljca_client *client;
  165. list_for_each_entry(client, &adap->client_list, link) {
  166. /*
  167. * Currently only GPIO register event callback, but
  168. * firmware message structure should include id when
  169. * multiple same type clients register event callback.
  170. */
  171. if (client->type == header->type) {
  172. unsigned long flags;
  173. spin_lock_irqsave(&client->event_cb_lock, flags);
  174. client->event_cb(client->context, header->cmd,
  175. header->data, header->len);
  176. spin_unlock_irqrestore(&client->event_cb_lock, flags);
  177. break;
  178. }
  179. }
  180. }
  181. /* process command ack and received data if available */
  182. static void ljca_handle_cmd_ack(struct ljca_adapter *adap, struct ljca_msg *header)
  183. {
  184. struct ljca_msg *tx_header = adap->tx_buf;
  185. u8 ibuf_len, actual_len = 0;
  186. unsigned long flags;
  187. u8 *ibuf;
  188. spin_lock_irqsave(&adap->lock, flags);
  189. if (tx_header->type != header->type || tx_header->cmd != header->cmd) {
  190. spin_unlock_irqrestore(&adap->lock, flags);
  191. dev_err(adap->dev, "cmd ack mismatch error\n");
  192. return;
  193. }
  194. ibuf_len = adap->ex_buf_len;
  195. ibuf = adap->ex_buf;
  196. if (ibuf && ibuf_len) {
  197. actual_len = min(header->len, ibuf_len);
  198. /* copy received data to external buffer */
  199. memcpy(ibuf, header->data, actual_len);
  200. }
  201. /* update copied data length */
  202. adap->actual_length = actual_len;
  203. spin_unlock_irqrestore(&adap->lock, flags);
  204. complete(&adap->cmd_completion);
  205. }
  206. static void ljca_recv(struct urb *urb)
  207. {
  208. struct ljca_msg *header = urb->transfer_buffer;
  209. struct ljca_adapter *adap = urb->context;
  210. int ret;
  211. switch (urb->status) {
  212. case 0:
  213. /* success */
  214. break;
  215. case -ENOENT:
  216. /*
  217. * directly complete the possible ongoing transfer
  218. * during disconnect
  219. */
  220. if (adap->disconnect)
  221. complete(&adap->cmd_completion);
  222. return;
  223. case -ECONNRESET:
  224. case -ESHUTDOWN:
  225. case -EPIPE:
  226. /* rx urb is terminated */
  227. dev_dbg(adap->dev, "rx urb terminated with status: %d\n",
  228. urb->status);
  229. return;
  230. default:
  231. dev_dbg(adap->dev, "rx urb error: %d\n", urb->status);
  232. goto resubmit;
  233. }
  234. if (header->len + sizeof(*header) != urb->actual_length)
  235. goto resubmit;
  236. if (header->flags & LJCA_ACK_FLAG)
  237. ljca_handle_cmd_ack(adap, header);
  238. else
  239. ljca_handle_event(adap, header);
  240. resubmit:
  241. ret = usb_submit_urb(urb, GFP_ATOMIC);
  242. if (ret && ret != -EPERM)
  243. dev_err(adap->dev, "resubmit rx urb error %d\n", ret);
  244. }
  245. static int ljca_send(struct ljca_adapter *adap, u8 type, u8 cmd,
  246. const u8 *obuf, u8 obuf_len, u8 *ibuf, u8 ibuf_len,
  247. bool ack, unsigned long timeout)
  248. {
  249. unsigned int msg_len = sizeof(struct ljca_msg) + obuf_len;
  250. struct ljca_msg *header = adap->tx_buf;
  251. unsigned int transferred;
  252. unsigned long flags;
  253. int ret;
  254. if (adap->disconnect)
  255. return -ENODEV;
  256. if (msg_len > adap->tx_buf_len)
  257. return -EINVAL;
  258. mutex_lock(&adap->mutex);
  259. spin_lock_irqsave(&adap->lock, flags);
  260. header->type = type;
  261. header->cmd = cmd;
  262. header->len = obuf_len;
  263. if (obuf)
  264. memcpy(header->data, obuf, obuf_len);
  265. header->flags = LJCA_CMPL_FLAG | (ack ? LJCA_ACK_FLAG : 0);
  266. adap->ex_buf = ibuf;
  267. adap->ex_buf_len = ibuf_len;
  268. adap->actual_length = 0;
  269. spin_unlock_irqrestore(&adap->lock, flags);
  270. reinit_completion(&adap->cmd_completion);
  271. ret = usb_autopm_get_interface(adap->intf);
  272. if (ret < 0)
  273. goto out;
  274. ret = usb_bulk_msg(adap->usb_dev, adap->tx_pipe, header,
  275. msg_len, &transferred, LJCA_WRITE_TIMEOUT_MS);
  276. if (ret < 0)
  277. goto out_put;
  278. if (transferred != msg_len) {
  279. ret = -EIO;
  280. goto out_put;
  281. }
  282. if (ack) {
  283. ret = wait_for_completion_timeout(&adap->cmd_completion,
  284. timeout);
  285. if (!ret) {
  286. ret = -ETIMEDOUT;
  287. goto out_put;
  288. }
  289. }
  290. ret = adap->actual_length;
  291. out_put:
  292. usb_autopm_put_interface(adap->intf);
  293. out:
  294. spin_lock_irqsave(&adap->lock, flags);
  295. adap->ex_buf = NULL;
  296. adap->ex_buf_len = 0;
  297. memset(header, 0, sizeof(*header));
  298. spin_unlock_irqrestore(&adap->lock, flags);
  299. mutex_unlock(&adap->mutex);
  300. return ret;
  301. }
  302. int ljca_transfer(struct ljca_client *client, u8 cmd, const u8 *obuf,
  303. u8 obuf_len, u8 *ibuf, u8 ibuf_len)
  304. {
  305. return ljca_send(client->adapter, client->type, cmd,
  306. obuf, obuf_len, ibuf, ibuf_len, true,
  307. LJCA_WRITE_ACK_TIMEOUT_MS);
  308. }
  309. EXPORT_SYMBOL_NS_GPL(ljca_transfer, LJCA);
  310. int ljca_transfer_noack(struct ljca_client *client, u8 cmd, const u8 *obuf,
  311. u8 obuf_len)
  312. {
  313. return ljca_send(client->adapter, client->type, cmd, obuf,
  314. obuf_len, NULL, 0, false, LJCA_WRITE_ACK_TIMEOUT_MS);
  315. }
  316. EXPORT_SYMBOL_NS_GPL(ljca_transfer_noack, LJCA);
  317. int ljca_register_event_cb(struct ljca_client *client, ljca_event_cb_t event_cb,
  318. void *context)
  319. {
  320. unsigned long flags;
  321. if (!event_cb)
  322. return -EINVAL;
  323. spin_lock_irqsave(&client->event_cb_lock, flags);
  324. if (client->event_cb) {
  325. spin_unlock_irqrestore(&client->event_cb_lock, flags);
  326. return -EALREADY;
  327. }
  328. client->event_cb = event_cb;
  329. client->context = context;
  330. spin_unlock_irqrestore(&client->event_cb_lock, flags);
  331. return 0;
  332. }
  333. EXPORT_SYMBOL_NS_GPL(ljca_register_event_cb, LJCA);
  334. void ljca_unregister_event_cb(struct ljca_client *client)
  335. {
  336. unsigned long flags;
  337. spin_lock_irqsave(&client->event_cb_lock, flags);
  338. client->event_cb = NULL;
  339. client->context = NULL;
  340. spin_unlock_irqrestore(&client->event_cb_lock, flags);
  341. }
  342. EXPORT_SYMBOL_NS_GPL(ljca_unregister_event_cb, LJCA);
  343. static int ljca_match_device_ids(struct acpi_device *adev, void *data)
  344. {
  345. struct ljca_match_ids_walk_data *wd = data;
  346. const char *uid = acpi_device_uid(adev);
  347. if (acpi_match_device_ids(adev, wd->ids))
  348. return 0;
  349. if (!wd->uid)
  350. goto match;
  351. if (!uid)
  352. /*
  353. * Some DSDTs have only one ACPI companion for the two I2C
  354. * controllers and they don't set a UID at all (e.g. Dell
  355. * Latitude 9420). On these platforms only the first I2C
  356. * controller is used, so if a HID match has no UID we use
  357. * "0" as the UID and assign ACPI companion to the first
  358. * I2C controller.
  359. */
  360. uid = "0";
  361. else
  362. uid = strchr(uid, wd->uid[0]);
  363. if (!uid || strcmp(uid, wd->uid))
  364. return 0;
  365. match:
  366. wd->adev = adev;
  367. return 1;
  368. }
  369. /* bind auxiliary device to acpi device */
  370. static void ljca_auxdev_acpi_bind(struct ljca_adapter *adap,
  371. struct auxiliary_device *auxdev,
  372. u64 adr, u8 id)
  373. {
  374. struct ljca_match_ids_walk_data wd = { 0 };
  375. struct device *dev = adap->dev;
  376. struct acpi_device *parent;
  377. char uid[4];
  378. parent = ACPI_COMPANION(dev);
  379. if (!parent)
  380. return;
  381. /*
  382. * Currently LJCA hw doesn't use _ADR instead the shipped
  383. * platforms use _HID to distinguish children devices.
  384. */
  385. switch (adr) {
  386. case LJCA_GPIO_ACPI_ADR:
  387. wd.ids = ljca_gpio_hids;
  388. break;
  389. case LJCA_I2C1_ACPI_ADR:
  390. case LJCA_I2C2_ACPI_ADR:
  391. snprintf(uid, sizeof(uid), "%d", id);
  392. wd.uid = uid;
  393. wd.ids = ljca_i2c_hids;
  394. break;
  395. case LJCA_SPI1_ACPI_ADR:
  396. case LJCA_SPI2_ACPI_ADR:
  397. wd.ids = ljca_spi_hids;
  398. break;
  399. default:
  400. dev_warn(dev, "unsupported _ADR\n");
  401. return;
  402. }
  403. acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd);
  404. if (wd.adev) {
  405. ACPI_COMPANION_SET(&auxdev->dev, wd.adev);
  406. return;
  407. }
  408. parent = ACPI_COMPANION(dev->parent->parent);
  409. if (!parent)
  410. return;
  411. acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd);
  412. if (wd.adev)
  413. ACPI_COMPANION_SET(&auxdev->dev, wd.adev);
  414. }
  415. static void ljca_auxdev_release(struct device *dev)
  416. {
  417. struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
  418. kfree(auxdev->dev.platform_data);
  419. }
  420. static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
  421. char *name, void *data, u64 adr)
  422. {
  423. struct auxiliary_device *auxdev;
  424. struct ljca_client *client;
  425. int ret;
  426. client = kzalloc(sizeof *client, GFP_KERNEL);
  427. if (!client) {
  428. kfree(data);
  429. return -ENOMEM;
  430. }
  431. client->type = type;
  432. client->id = id;
  433. client->adapter = adap;
  434. spin_lock_init(&client->event_cb_lock);
  435. auxdev = &client->auxdev;
  436. auxdev->name = name;
  437. auxdev->id = id;
  438. auxdev->dev.parent = adap->dev;
  439. auxdev->dev.platform_data = data;
  440. auxdev->dev.release = ljca_auxdev_release;
  441. ret = auxiliary_device_init(auxdev);
  442. if (ret) {
  443. kfree(data);
  444. goto err_free;
  445. }
  446. ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
  447. ret = auxiliary_device_add(auxdev);
  448. if (ret)
  449. goto err_uninit;
  450. list_add_tail(&client->link, &adap->client_list);
  451. return 0;
  452. err_uninit:
  453. auxiliary_device_uninit(auxdev);
  454. err_free:
  455. kfree(client);
  456. return ret;
  457. }
  458. static int ljca_enumerate_gpio(struct ljca_adapter *adap)
  459. {
  460. u32 valid_pin[LJCA_MAX_GPIO_NUM / BITS_PER_TYPE(u32)];
  461. struct ljca_gpio_descriptor *desc;
  462. struct ljca_gpio_info *gpio_info;
  463. u8 buf[LJCA_MAX_PAYLOAD_SIZE];
  464. int ret, gpio_num;
  465. unsigned int i;
  466. ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_GPIO, NULL, 0, buf,
  467. sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
  468. if (ret < 0)
  469. return ret;
  470. /* check firmware response */
  471. desc = (struct ljca_gpio_descriptor *)buf;
  472. if (ret != struct_size(desc, bank_desc, desc->bank_num))
  473. return -EINVAL;
  474. gpio_num = desc->pins_per_bank * desc->bank_num;
  475. if (gpio_num > LJCA_MAX_GPIO_NUM)
  476. return -EINVAL;
  477. /* construct platform data */
  478. gpio_info = kzalloc(sizeof *gpio_info, GFP_KERNEL);
  479. if (!gpio_info)
  480. return -ENOMEM;
  481. gpio_info->num = gpio_num;
  482. for (i = 0; i < desc->bank_num; i++)
  483. valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
  484. bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
  485. return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
  486. gpio_info, LJCA_GPIO_ACPI_ADR);
  487. }
  488. static int ljca_enumerate_i2c(struct ljca_adapter *adap)
  489. {
  490. struct ljca_i2c_descriptor *desc;
  491. struct ljca_i2c_info *i2c_info;
  492. u8 buf[LJCA_MAX_PAYLOAD_SIZE];
  493. unsigned int i;
  494. int ret;
  495. ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_I2C, NULL, 0, buf,
  496. sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
  497. if (ret < 0)
  498. return ret;
  499. /* check firmware response */
  500. desc = (struct ljca_i2c_descriptor *)buf;
  501. if (ret != struct_size(desc, info, desc->num))
  502. return -EINVAL;
  503. for (i = 0; i < desc->num; i++) {
  504. /* construct platform data */
  505. i2c_info = kzalloc(sizeof *i2c_info, GFP_KERNEL);
  506. if (!i2c_info)
  507. return -ENOMEM;
  508. i2c_info->id = desc->info[i].id;
  509. i2c_info->capacity = desc->info[i].capacity;
  510. i2c_info->intr_pin = desc->info[i].intr_pin;
  511. ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
  512. "ljca-i2c", i2c_info,
  513. LJCA_I2C1_ACPI_ADR + i);
  514. if (ret)
  515. return ret;
  516. }
  517. return 0;
  518. }
  519. static int ljca_enumerate_spi(struct ljca_adapter *adap)
  520. {
  521. struct ljca_spi_descriptor *desc;
  522. struct ljca_spi_info *spi_info;
  523. u8 buf[LJCA_MAX_PAYLOAD_SIZE];
  524. unsigned int i;
  525. int ret;
  526. /* Not all LJCA chips implement SPI, a timeout reading the descriptors is normal */
  527. ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_SPI, NULL, 0, buf,
  528. sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS);
  529. if (ret < 0)
  530. return (ret == -ETIMEDOUT) ? 0 : ret;
  531. /* check firmware response */
  532. desc = (struct ljca_spi_descriptor *)buf;
  533. if (ret != struct_size(desc, info, desc->num))
  534. return -EINVAL;
  535. for (i = 0; i < desc->num; i++) {
  536. /* construct platform data */
  537. spi_info = kzalloc(sizeof *spi_info, GFP_KERNEL);
  538. if (!spi_info)
  539. return -ENOMEM;
  540. spi_info->id = desc->info[i].id;
  541. spi_info->capacity = desc->info[i].capacity;
  542. ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
  543. "ljca-spi", spi_info,
  544. LJCA_SPI1_ACPI_ADR + i);
  545. if (ret)
  546. return ret;
  547. }
  548. return 0;
  549. }
  550. static int ljca_reset_handshake(struct ljca_adapter *adap)
  551. {
  552. __le32 reset_id = cpu_to_le32(adap->reset_id);
  553. __le32 reset_id_ret = 0;
  554. int ret;
  555. adap->reset_id++;
  556. ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_RESET, (u8 *)&reset_id,
  557. sizeof(__le32), (u8 *)&reset_id_ret, sizeof(__le32),
  558. true, LJCA_WRITE_ACK_TIMEOUT_MS);
  559. if (ret < 0)
  560. return ret;
  561. if (reset_id_ret != reset_id)
  562. return -EINVAL;
  563. return 0;
  564. }
  565. static int ljca_enumerate_clients(struct ljca_adapter *adap)
  566. {
  567. struct ljca_client *client, *next;
  568. int ret;
  569. ret = ljca_reset_handshake(adap);
  570. if (ret)
  571. goto err_kill;
  572. ret = ljca_enumerate_gpio(adap);
  573. if (ret) {
  574. dev_err(adap->dev, "enumerate GPIO error\n");
  575. goto err_kill;
  576. }
  577. ret = ljca_enumerate_i2c(adap);
  578. if (ret) {
  579. dev_err(adap->dev, "enumerate I2C error\n");
  580. goto err_kill;
  581. }
  582. ret = ljca_enumerate_spi(adap);
  583. if (ret) {
  584. dev_err(adap->dev, "enumerate SPI error\n");
  585. goto err_kill;
  586. }
  587. return 0;
  588. err_kill:
  589. adap->disconnect = true;
  590. usb_kill_urb(adap->rx_urb);
  591. list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) {
  592. auxiliary_device_delete(&client->auxdev);
  593. auxiliary_device_uninit(&client->auxdev);
  594. list_del_init(&client->link);
  595. kfree(client);
  596. }
  597. return ret;
  598. }
  599. static int ljca_probe(struct usb_interface *interface,
  600. const struct usb_device_id *id)
  601. {
  602. struct usb_device *usb_dev = interface_to_usbdev(interface);
  603. struct usb_host_interface *alt = interface->cur_altsetting;
  604. struct usb_endpoint_descriptor *ep_in, *ep_out;
  605. struct device *dev = &interface->dev;
  606. struct ljca_adapter *adap;
  607. int ret;
  608. adap = devm_kzalloc(dev, sizeof(*adap), GFP_KERNEL);
  609. if (!adap)
  610. return -ENOMEM;
  611. /* separate tx buffer allocation for alignment */
  612. adap->tx_buf = devm_kzalloc(dev, LJCA_MAX_PACKET_SIZE, GFP_KERNEL);
  613. if (!adap->tx_buf)
  614. return -ENOMEM;
  615. adap->tx_buf_len = LJCA_MAX_PACKET_SIZE;
  616. mutex_init(&adap->mutex);
  617. spin_lock_init(&adap->lock);
  618. init_completion(&adap->cmd_completion);
  619. INIT_LIST_HEAD(&adap->client_list);
  620. adap->intf = usb_get_intf(interface);
  621. adap->usb_dev = usb_dev;
  622. adap->dev = dev;
  623. /*
  624. * find the first bulk in and out endpoints.
  625. * ignore any others.
  626. */
  627. ret = usb_find_common_endpoints(alt, &ep_in, &ep_out, NULL, NULL);
  628. if (ret) {
  629. dev_err(dev, "bulk endpoints not found\n");
  630. goto err_put;
  631. }
  632. adap->rx_pipe = usb_rcvbulkpipe(usb_dev, usb_endpoint_num(ep_in));
  633. adap->tx_pipe = usb_sndbulkpipe(usb_dev, usb_endpoint_num(ep_out));
  634. /* setup rx buffer */
  635. adap->rx_len = usb_endpoint_maxp(ep_in);
  636. adap->rx_buf = devm_kzalloc(dev, adap->rx_len, GFP_KERNEL);
  637. if (!adap->rx_buf) {
  638. ret = -ENOMEM;
  639. goto err_put;
  640. }
  641. /* alloc rx urb */
  642. adap->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  643. if (!adap->rx_urb) {
  644. ret = -ENOMEM;
  645. goto err_put;
  646. }
  647. usb_fill_bulk_urb(adap->rx_urb, usb_dev, adap->rx_pipe,
  648. adap->rx_buf, adap->rx_len, ljca_recv, adap);
  649. usb_set_intfdata(interface, adap);
  650. /* submit rx urb before enumerate clients */
  651. ret = usb_submit_urb(adap->rx_urb, GFP_KERNEL);
  652. if (ret) {
  653. dev_err(dev, "submit rx urb failed: %d\n", ret);
  654. goto err_free;
  655. }
  656. ret = ljca_enumerate_clients(adap);
  657. if (ret)
  658. goto err_free;
  659. /*
  660. * This works around problems with ov2740 initialization on some
  661. * Lenovo platforms. The autosuspend delay, has to be smaller than
  662. * the delay after setting the reset_gpio line in ov2740_resume().
  663. * Otherwise the sensor randomly fails to initialize.
  664. */
  665. pm_runtime_set_autosuspend_delay(&usb_dev->dev, 10);
  666. usb_enable_autosuspend(usb_dev);
  667. return 0;
  668. err_free:
  669. usb_free_urb(adap->rx_urb);
  670. err_put:
  671. usb_put_intf(adap->intf);
  672. mutex_destroy(&adap->mutex);
  673. return ret;
  674. }
  675. static void ljca_disconnect(struct usb_interface *interface)
  676. {
  677. struct ljca_adapter *adap = usb_get_intfdata(interface);
  678. struct ljca_client *client, *next;
  679. adap->disconnect = true;
  680. usb_kill_urb(adap->rx_urb);
  681. list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) {
  682. auxiliary_device_delete(&client->auxdev);
  683. auxiliary_device_uninit(&client->auxdev);
  684. list_del_init(&client->link);
  685. kfree(client);
  686. }
  687. usb_free_urb(adap->rx_urb);
  688. usb_put_intf(adap->intf);
  689. mutex_destroy(&adap->mutex);
  690. }
  691. static int ljca_suspend(struct usb_interface *interface, pm_message_t message)
  692. {
  693. struct ljca_adapter *adap = usb_get_intfdata(interface);
  694. usb_kill_urb(adap->rx_urb);
  695. return 0;
  696. }
  697. static int ljca_resume(struct usb_interface *interface)
  698. {
  699. struct ljca_adapter *adap = usb_get_intfdata(interface);
  700. return usb_submit_urb(adap->rx_urb, GFP_KERNEL);
  701. }
  702. static const struct usb_device_id ljca_table[] = {
  703. { USB_DEVICE(0x8086, 0x0b63) },
  704. { /* sentinel */ }
  705. };
  706. MODULE_DEVICE_TABLE(usb, ljca_table);
  707. static struct usb_driver ljca_driver = {
  708. .name = "ljca",
  709. .id_table = ljca_table,
  710. .probe = ljca_probe,
  711. .disconnect = ljca_disconnect,
  712. .suspend = ljca_suspend,
  713. .resume = ljca_resume,
  714. .supports_autosuspend = 1,
  715. };
  716. module_usb_driver(ljca_driver);
  717. MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>");
  718. MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>");
  719. MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>");
  720. MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB driver");
  721. MODULE_LICENSE("GPL");