ccid_bridge.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt "\n", __func__
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/usb.h>
  20. #include <linux/wait.h>
  21. #include <linux/cdev.h>
  22. #include <linux/ktime.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/usb/ccid_bridge.h>
  25. #define CCID_CLASS_DECRIPTOR_TYPE 0x21
  26. #define CCID_NOTIFY_SLOT_CHANGE 0x50
  27. #define CCID_NOTIFY_HARDWARE_ERROR 0x51
  28. #define CCID_ABORT_REQ 0x1
  29. #define CCID_GET_CLK_FREQ_REQ 0x2
  30. #define CCID_GET_DATA_RATES 0x3
  31. #define CCID_BRIDGE_MSG_SZ 512
  32. #define CCID_BRIDGE_OPEN_TIMEOUT 500 /* msec */
  33. #define CCID_CONTROL_TIMEOUT 500 /* msec */
  34. #define CCID_BRIDGE_MSG_TIMEOUT 1000 /* msec */
  35. static unsigned ccid_bulk_msg_timeout = CCID_BRIDGE_MSG_TIMEOUT;
  36. module_param_named(bulk_msg_timeout, ccid_bulk_msg_timeout, uint, 0644);
  37. MODULE_PARM_DESC(bulk_msg_timeout, "Bulk message timeout (msecs)");
  38. struct ccid_bridge {
  39. struct usb_device *udev;
  40. struct usb_interface *intf;
  41. unsigned int in_pipe;
  42. unsigned int out_pipe;
  43. unsigned int int_pipe;
  44. struct urb *inturb;
  45. struct urb *readurb;
  46. struct urb *writeurb;
  47. bool opened;
  48. bool events_supported;
  49. bool is_suspended;
  50. struct mutex open_mutex;
  51. struct mutex write_mutex;
  52. struct mutex read_mutex;
  53. struct mutex event_mutex;
  54. int write_result;
  55. int read_result;
  56. int event_result;
  57. wait_queue_head_t open_wq;
  58. wait_queue_head_t write_wq;
  59. wait_queue_head_t read_wq;
  60. wait_queue_head_t event_wq;
  61. struct usb_ccid_event cur_event;
  62. void *intbuf;
  63. dev_t chrdev;
  64. struct cdev cdev;
  65. struct class *class;
  66. struct device *device;
  67. struct dentry *dbg_root;
  68. unsigned n_write;
  69. unsigned n_read;
  70. unsigned n_write_timeout;
  71. unsigned n_read_timeout;
  72. unsigned long write_max_time;
  73. unsigned long read_max_time;
  74. };
  75. static struct ccid_bridge *__ccid_bridge_dev;
  76. static void ccid_bridge_out_cb(struct urb *urb)
  77. {
  78. struct ccid_bridge *ccid = urb->context;
  79. if (urb->dev->state == USB_STATE_NOTATTACHED)
  80. ccid->write_result = -ENODEV;
  81. else
  82. ccid->write_result = urb->status ? : urb->actual_length;
  83. pr_debug("write result = %d", ccid->write_result);
  84. wake_up(&ccid->write_wq);
  85. }
  86. static void ccid_bridge_in_cb(struct urb *urb)
  87. {
  88. struct ccid_bridge *ccid = urb->context;
  89. if (urb->dev->state == USB_STATE_NOTATTACHED)
  90. ccid->read_result = -ENODEV;
  91. else
  92. ccid->read_result = urb->status ? : urb->actual_length;
  93. pr_debug("read result = %d", ccid->read_result);
  94. wake_up(&ccid->read_wq);
  95. }
  96. static void ccid_bridge_int_cb(struct urb *urb)
  97. {
  98. struct ccid_bridge *ccid = urb->context;
  99. u8 *msg_type;
  100. bool wakeup = true;
  101. if (urb->dev->state == USB_STATE_NOTATTACHED || (urb->status &&
  102. urb->status != -ENOENT)) {
  103. ccid->event_result = -ENODEV;
  104. wakeup = true;
  105. goto out;
  106. }
  107. /*
  108. * Don't wakeup the event ioctl process during suspend.
  109. * The suspend state is not visible to user space.
  110. * we wake up the process after resume to send RESUME
  111. * event if the device supports remote wakeup.
  112. */
  113. if (urb->status == -ENOENT && !urb->actual_length) {
  114. ccid->event_result = -ENOENT;
  115. wakeup = false;
  116. goto out;
  117. }
  118. ccid->event_result = 0;
  119. msg_type = urb->transfer_buffer;
  120. switch (*msg_type) {
  121. case CCID_NOTIFY_SLOT_CHANGE:
  122. pr_debug("NOTIFY_SLOT_CHANGE event arrived");
  123. ccid->cur_event.event = USB_CCID_NOTIFY_SLOT_CHANGE_EVENT;
  124. ccid->cur_event.u.notify.slot_icc_state = *(++msg_type);
  125. break;
  126. case CCID_NOTIFY_HARDWARE_ERROR:
  127. pr_debug("NOTIFY_HARDWARE_ERROR event arrived");
  128. ccid->cur_event.event = USB_CCID_HARDWARE_ERROR_EVENT;
  129. ccid->cur_event.u.error.slot = *(++msg_type);
  130. ccid->cur_event.u.error.seq = *(++msg_type);
  131. ccid->cur_event.u.error.error_code = *(++msg_type);
  132. break;
  133. default:
  134. pr_err("UNKNOWN event arrived\n");
  135. ccid->event_result = -EINVAL;
  136. }
  137. out:
  138. pr_debug("returning %d", ccid->event_result);
  139. if (wakeup)
  140. wake_up(&ccid->event_wq);
  141. }
  142. static int ccid_bridge_submit_inturb(struct ccid_bridge *ccid)
  143. {
  144. int ret = 0;
  145. /*
  146. * Don't resume the bus to submit an interrupt URB.
  147. * We submit the URB in resume path. This is important.
  148. * Because the device will be in suspend state during
  149. * multiple system suspend/resume cycles. The user space
  150. * process comes here during system resume after it is
  151. * unfrozen.
  152. */
  153. if (!ccid->int_pipe || ccid->is_suspended)
  154. goto out;
  155. ret = usb_autopm_get_interface(ccid->intf);
  156. if (ret < 0) {
  157. pr_debug("fail to get autopm with %d\n", ret);
  158. goto out;
  159. }
  160. ret = usb_submit_urb(ccid->inturb, GFP_KERNEL);
  161. if (ret < 0)
  162. pr_err("fail to submit int urb with %d\n", ret);
  163. usb_autopm_put_interface(ccid->intf);
  164. out:
  165. pr_debug("returning %d", ret);
  166. return ret;
  167. }
  168. static int ccid_bridge_get_event(struct ccid_bridge *ccid)
  169. {
  170. int ret = 0;
  171. /*
  172. * The first event returned after the device resume
  173. * will be RESUME event. This event is set by
  174. * the resume.
  175. */
  176. if (ccid->cur_event.event)
  177. goto out;
  178. ccid->event_result = -EINPROGRESS;
  179. ret = ccid_bridge_submit_inturb(ccid);
  180. if (ret < 0)
  181. goto out;
  182. /*
  183. * Wait for the notification on interrupt endpoint
  184. * or remote wakeup event from the resume. The
  185. * int urb completion handler and resume callback
  186. * take care of setting the current event.
  187. */
  188. mutex_unlock(&ccid->event_mutex);
  189. ret = wait_event_interruptible(ccid->event_wq,
  190. (ccid->event_result != -EINPROGRESS));
  191. mutex_lock(&ccid->event_mutex);
  192. if (ret == -ERESTARTSYS) /* interrupted */
  193. usb_kill_urb(ccid->inturb);
  194. else
  195. ret = ccid->event_result;
  196. out:
  197. pr_debug("returning %d", ret);
  198. return ret;
  199. }
  200. static int ccid_bridge_open(struct inode *ip, struct file *fp)
  201. {
  202. struct ccid_bridge *ccid = container_of(ip->i_cdev,
  203. struct ccid_bridge, cdev);
  204. int ret;
  205. pr_debug("called");
  206. mutex_lock(&ccid->open_mutex);
  207. if (ccid->opened) {
  208. ret = -EBUSY;
  209. goto out;
  210. }
  211. mutex_unlock(&ccid->open_mutex);
  212. ret = wait_event_interruptible_timeout(ccid->open_wq,
  213. ccid->intf != NULL, msecs_to_jiffies(
  214. CCID_BRIDGE_OPEN_TIMEOUT));
  215. mutex_lock(&ccid->open_mutex);
  216. if (ret != -ERESTARTSYS && ccid->intf) {
  217. fp->private_data = ccid;
  218. ccid->opened = true;
  219. ret = 0;
  220. } else if (!ret) { /* timed out */
  221. ret = -ENODEV;
  222. }
  223. out:
  224. mutex_unlock(&ccid->open_mutex);
  225. pr_debug("returning %d", ret);
  226. return ret;
  227. }
  228. static ssize_t ccid_bridge_write(struct file *fp, const char __user *ubuf,
  229. size_t count, loff_t *pos)
  230. {
  231. struct ccid_bridge *ccid = fp->private_data;
  232. int ret;
  233. char *kbuf;
  234. ktime_t start_t, delta_t;
  235. pr_debug("called with %d", count);
  236. if (!ccid->intf) {
  237. pr_debug("intf is not active");
  238. return -ENODEV;
  239. }
  240. mutex_lock(&ccid->write_mutex);
  241. ccid->n_write++;
  242. start_t = ktime_get();
  243. if (!count || count > CCID_BRIDGE_MSG_SZ) {
  244. pr_err("invalid count");
  245. ret = -EINVAL;
  246. goto out;
  247. }
  248. kbuf = kmalloc(count, GFP_KERNEL);
  249. if (!kbuf) {
  250. pr_err("fail to allocate memory");
  251. ret = -ENOMEM;
  252. goto out;
  253. }
  254. ret = copy_from_user(kbuf, ubuf, count);
  255. if (ret) {
  256. pr_err("fail to copy user buf");
  257. ret = -EFAULT;
  258. goto free_kbuf;
  259. }
  260. ret = usb_autopm_get_interface(ccid->intf);
  261. if (ret) {
  262. pr_err("fail to get autopm with %d", ret);
  263. goto free_kbuf;
  264. }
  265. ccid->write_result = 0;
  266. usb_fill_bulk_urb(ccid->writeurb, ccid->udev, ccid->out_pipe,
  267. kbuf, count, ccid_bridge_out_cb, ccid);
  268. ret = usb_submit_urb(ccid->writeurb, GFP_KERNEL);
  269. if (ret < 0) {
  270. pr_err("urb submit fail with %d", ret);
  271. goto put_pm;
  272. }
  273. ret = wait_event_interruptible_timeout(ccid->write_wq,
  274. ccid->write_result != 0,
  275. msecs_to_jiffies(ccid_bulk_msg_timeout));
  276. if (!ret || ret == -ERESTARTSYS) { /* timedout or interrupted */
  277. usb_kill_urb(ccid->writeurb);
  278. if (!ret) {
  279. ccid->n_write_timeout++;
  280. ret = -ETIMEDOUT;
  281. }
  282. } else {
  283. ret = ccid->write_result;
  284. }
  285. if (ret >= 0) {
  286. delta_t = ktime_sub(ktime_get(), start_t);
  287. if (ktime_to_ms(delta_t) > ccid->write_max_time)
  288. ccid->write_max_time = ktime_to_ms(delta_t);
  289. }
  290. pr_debug("returning %d", ret);
  291. put_pm:
  292. if (ret != -ENODEV)
  293. usb_autopm_put_interface(ccid->intf);
  294. free_kbuf:
  295. kfree(kbuf);
  296. out:
  297. mutex_unlock(&ccid->write_mutex);
  298. return ret;
  299. }
  300. static ssize_t ccid_bridge_read(struct file *fp, char __user *ubuf,
  301. size_t count, loff_t *pos)
  302. {
  303. struct ccid_bridge *ccid = fp->private_data;
  304. int ret;
  305. char *kbuf;
  306. ktime_t start_t, delta_t;
  307. pr_debug("called with %d", count);
  308. if (!ccid->intf) {
  309. pr_debug("intf is not active");
  310. return -ENODEV;
  311. }
  312. mutex_lock(&ccid->read_mutex);
  313. ccid->n_read++;
  314. start_t = ktime_get();
  315. if (!count || count > CCID_BRIDGE_MSG_SZ) {
  316. pr_err("invalid count");
  317. ret = -EINVAL;
  318. goto out;
  319. }
  320. kbuf = kmalloc(count, GFP_KERNEL);
  321. if (!kbuf) {
  322. pr_err("fail to allocate memory");
  323. ret = -ENOMEM;
  324. goto out;
  325. }
  326. ret = usb_autopm_get_interface(ccid->intf);
  327. if (ret) {
  328. pr_err("fail to get autopm with %d", ret);
  329. goto free_kbuf;
  330. }
  331. ccid->read_result = 0;
  332. usb_fill_bulk_urb(ccid->readurb, ccid->udev, ccid->in_pipe,
  333. kbuf, count, ccid_bridge_in_cb, ccid);
  334. ret = usb_submit_urb(ccid->readurb, GFP_KERNEL);
  335. if (ret < 0) {
  336. pr_err("urb submit fail with %d", ret);
  337. if (ret != -ENODEV)
  338. usb_autopm_put_interface(ccid->intf);
  339. goto free_kbuf;
  340. }
  341. ret = wait_event_interruptible_timeout(ccid->read_wq,
  342. ccid->read_result != 0,
  343. msecs_to_jiffies(ccid_bulk_msg_timeout));
  344. if (!ret || ret == -ERESTARTSYS) { /* timedout or interrupted */
  345. usb_kill_urb(ccid->readurb);
  346. if (!ret) {
  347. ccid->n_read_timeout++;
  348. ret = -ETIMEDOUT;
  349. }
  350. } else {
  351. ret = ccid->read_result;
  352. }
  353. if (ret > 0) {
  354. if (copy_to_user(ubuf, kbuf, ret))
  355. ret = -EFAULT;
  356. delta_t = ktime_sub(ktime_get(), start_t);
  357. if (ktime_to_ms(delta_t) > ccid->read_max_time)
  358. ccid->read_max_time = ktime_to_ms(delta_t);
  359. }
  360. usb_autopm_put_interface(ccid->intf);
  361. pr_debug("returning %d", ret);
  362. free_kbuf:
  363. kfree(kbuf);
  364. out:
  365. mutex_unlock(&ccid->read_mutex);
  366. return ret;
  367. }
  368. static long
  369. ccid_bridge_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  370. {
  371. struct ccid_bridge *ccid = fp->private_data;
  372. char *buf;
  373. struct usb_ccid_data data;
  374. struct usb_ccid_abort abort;
  375. struct usb_descriptor_header *header;
  376. int ret;
  377. struct usb_device *udev = ccid->udev;
  378. __u8 intf = ccid->intf->cur_altsetting->desc.bInterfaceNumber;
  379. __u8 breq = 0;
  380. if (!ccid->intf) {
  381. pr_debug("intf is not active");
  382. return -ENODEV;
  383. }
  384. mutex_lock(&ccid->event_mutex);
  385. switch (cmd) {
  386. case USB_CCID_GET_CLASS_DESC:
  387. pr_debug("GET_CLASS_DESC ioctl called");
  388. ret = copy_from_user(&data, (void __user *)arg, sizeof(data));
  389. if (ret) {
  390. ret = -EFAULT;
  391. break;
  392. }
  393. ret = __usb_get_extra_descriptor(udev->rawdescriptors[0],
  394. le16_to_cpu(udev->config[0].desc.wTotalLength),
  395. CCID_CLASS_DECRIPTOR_TYPE, (void **) &buf, sizeof(*buf));
  396. if (ret) {
  397. ret = -ENOENT;
  398. break;
  399. }
  400. header = (struct usb_descriptor_header *) buf;
  401. if (data.length != header->bLength) {
  402. ret = -EINVAL;
  403. break;
  404. }
  405. ret = copy_to_user((void __user *)data.data, buf, data.length);
  406. if (ret)
  407. ret = -EFAULT;
  408. break;
  409. case USB_CCID_GET_CLOCK_FREQUENCIES:
  410. pr_debug("GET_CLOCK_FREQUENCIES ioctl called");
  411. breq = CCID_GET_CLK_FREQ_REQ;
  412. /* fall through */
  413. case USB_CCID_GET_DATA_RATES:
  414. if (!breq) {
  415. pr_debug("GET_DATA_RATES ioctl called");
  416. breq = CCID_GET_DATA_RATES;
  417. }
  418. ret = copy_from_user(&data, (void __user *)arg, sizeof(data));
  419. if (ret) {
  420. ret = -EFAULT;
  421. break;
  422. }
  423. buf = kmalloc(data.length, GFP_KERNEL);
  424. if (!buf) {
  425. ret = -ENOMEM;
  426. break;
  427. }
  428. ret = usb_autopm_get_interface(ccid->intf);
  429. if (ret < 0) {
  430. pr_debug("fail to get autopm with %d", ret);
  431. break;
  432. }
  433. ret = usb_control_msg(ccid->udev,
  434. usb_rcvctrlpipe(ccid->udev, 0),
  435. breq, (USB_DIR_IN | USB_TYPE_CLASS |
  436. USB_RECIP_INTERFACE), 0, intf, buf,
  437. data.length, CCID_CONTROL_TIMEOUT);
  438. usb_autopm_put_interface(ccid->intf);
  439. if (ret == data.length) {
  440. ret = copy_to_user((void __user *)data.data, buf,
  441. data.length);
  442. if (ret)
  443. ret = -EFAULT;
  444. } else {
  445. if (ret > 0)
  446. ret = -EPIPE;
  447. }
  448. kfree(buf);
  449. break;
  450. case USB_CCID_ABORT:
  451. pr_debug("ABORT ioctl called");
  452. breq = CCID_ABORT_REQ;
  453. ret = copy_from_user(&abort, (void __user *)arg, sizeof(abort));
  454. if (ret) {
  455. ret = -EFAULT;
  456. break;
  457. }
  458. ret = usb_autopm_get_interface(ccid->intf);
  459. if (ret < 0) {
  460. pr_debug("fail to get autopm with %d", ret);
  461. break;
  462. }
  463. ret = usb_control_msg(ccid->udev,
  464. usb_sndctrlpipe(ccid->udev, 0),
  465. breq, (USB_DIR_OUT | USB_TYPE_CLASS |
  466. USB_RECIP_INTERFACE),
  467. (abort.seq << 8) | abort.slot, intf, NULL,
  468. 0, CCID_CONTROL_TIMEOUT);
  469. if (ret < 0)
  470. pr_err("abort request failed with err %d\n", ret);
  471. usb_autopm_put_interface(ccid->intf);
  472. break;
  473. case USB_CCID_GET_EVENT:
  474. pr_debug("GET_EVENT ioctl called");
  475. if (!ccid->events_supported) {
  476. ret = -ENOENT;
  477. break;
  478. }
  479. ret = ccid_bridge_get_event(ccid);
  480. if (ret == 0) {
  481. ret = copy_to_user((void __user *)arg, &ccid->cur_event,
  482. sizeof(ccid->cur_event));
  483. if (ret)
  484. ret = -EFAULT;
  485. }
  486. ccid->cur_event.event = 0;
  487. break;
  488. default:
  489. pr_err("UNKNOWN ioctl called");
  490. ret = -EINVAL;
  491. break;
  492. }
  493. mutex_unlock(&ccid->event_mutex);
  494. pr_debug("returning %d", ret);
  495. return ret;
  496. }
  497. static void ccid_bridge_reset_stats(struct ccid_bridge *ccid)
  498. {
  499. ccid->n_write = 0;
  500. ccid->n_read = 0;
  501. ccid->n_write_timeout = 0;
  502. ccid->n_read_timeout = 0;
  503. ccid->write_max_time = 0;
  504. ccid->read_max_time = 0;
  505. }
  506. static int ccid_bridge_release(struct inode *ip, struct file *fp)
  507. {
  508. struct ccid_bridge *ccid = fp->private_data;
  509. pr_debug("called");
  510. mutex_lock(&ccid->open_mutex);
  511. if (ccid->intf == NULL) {
  512. ccid->opened = false;
  513. mutex_unlock(&ccid->open_mutex);
  514. goto done;
  515. }
  516. mutex_unlock(&ccid->open_mutex);
  517. usb_kill_urb(ccid->writeurb);
  518. usb_kill_urb(ccid->readurb);
  519. if (ccid->int_pipe)
  520. usb_kill_urb(ccid->inturb);
  521. ccid->event_result = -EIO;
  522. wake_up(&ccid->event_wq);
  523. mutex_lock(&ccid->open_mutex);
  524. ccid->opened = false;
  525. mutex_unlock(&ccid->open_mutex);
  526. done:
  527. ccid_bridge_reset_stats(ccid);
  528. return 0;
  529. }
  530. static const struct file_operations ccid_bridge_fops = {
  531. .owner = THIS_MODULE,
  532. .open = ccid_bridge_open,
  533. .write = ccid_bridge_write,
  534. .read = ccid_bridge_read,
  535. .unlocked_ioctl = ccid_bridge_ioctl,
  536. .release = ccid_bridge_release,
  537. };
  538. static int ccid_bridge_suspend(struct usb_interface *intf, pm_message_t message)
  539. {
  540. struct ccid_bridge *ccid = usb_get_intfdata(intf);
  541. int ret = 0;
  542. pr_debug("called");
  543. if (!ccid->opened)
  544. goto out;
  545. mutex_lock(&ccid->event_mutex);
  546. if (ccid->int_pipe) {
  547. usb_kill_urb(ccid->inturb);
  548. if (ccid->event_result != -ENOENT) {
  549. ret = -EBUSY;
  550. goto rel_mutex;
  551. }
  552. }
  553. ccid->is_suspended = true;
  554. rel_mutex:
  555. mutex_unlock(&ccid->event_mutex);
  556. out:
  557. pr_debug("returning %d", ret);
  558. return ret;
  559. }
  560. static int ccid_bridge_resume(struct usb_interface *intf)
  561. {
  562. struct ccid_bridge *ccid = usb_get_intfdata(intf);
  563. int ret;
  564. pr_debug("called");
  565. if (!ccid->opened)
  566. goto out;
  567. mutex_lock(&ccid->event_mutex);
  568. ccid->is_suspended = false;
  569. if (device_can_wakeup(&ccid->udev->dev)) {
  570. ccid->event_result = 0;
  571. ccid->cur_event.event = USB_CCID_RESUME_EVENT;
  572. wake_up(&ccid->event_wq);
  573. } else if (ccid->int_pipe) {
  574. ccid->event_result = -EINPROGRESS;
  575. ret = usb_submit_urb(ccid->inturb, GFP_KERNEL);
  576. if (ret < 0)
  577. pr_debug("fail to submit inturb with %d\n", ret);
  578. }
  579. mutex_unlock(&ccid->event_mutex);
  580. out:
  581. return 0;
  582. }
  583. static int
  584. ccid_bridge_probe(struct usb_interface *intf, const struct usb_device_id *id)
  585. {
  586. struct ccid_bridge *ccid = __ccid_bridge_dev;
  587. struct usb_host_interface *intf_desc;
  588. struct usb_endpoint_descriptor *ep_desc;
  589. struct usb_host_endpoint *ep;
  590. __u8 epin_addr = 0, epout_addr = 0, epint_addr = 0;
  591. int i, ret;
  592. intf_desc = intf->cur_altsetting;
  593. if (intf_desc->desc.bNumEndpoints > 3)
  594. return -ENODEV;
  595. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  596. ep_desc = &intf_desc->endpoint[i].desc;
  597. if (usb_endpoint_is_bulk_in(ep_desc))
  598. epin_addr = ep_desc->bEndpointAddress;
  599. else if (usb_endpoint_is_bulk_out(ep_desc))
  600. epout_addr = ep_desc->bEndpointAddress;
  601. else if (usb_endpoint_is_int_in(ep_desc))
  602. epint_addr = ep_desc->bEndpointAddress;
  603. else
  604. return -ENODEV;
  605. }
  606. if (!epin_addr || !epout_addr)
  607. return -ENODEV;
  608. ccid->udev = usb_get_dev(interface_to_usbdev(intf));
  609. ccid->in_pipe = usb_rcvbulkpipe(ccid->udev, epin_addr);
  610. ccid->out_pipe = usb_sndbulkpipe(ccid->udev, epout_addr);
  611. if (epint_addr)
  612. ccid->int_pipe = usb_rcvbulkpipe(ccid->udev, epint_addr);
  613. ccid->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  614. if (!ccid->writeurb) {
  615. pr_err("fail to allocate write urb");
  616. ret = -ENOMEM;
  617. goto put_udev;
  618. }
  619. ccid->readurb = usb_alloc_urb(0, GFP_KERNEL);
  620. if (!ccid->readurb) {
  621. pr_err("fail to allocate read urb");
  622. ret = -ENOMEM;
  623. goto free_writeurb;
  624. }
  625. if (ccid->int_pipe) {
  626. pr_debug("interrupt endpoint is present");
  627. ep = usb_pipe_endpoint(ccid->udev, ccid->int_pipe);
  628. ccid->inturb = usb_alloc_urb(0, GFP_KERNEL);
  629. if (!ccid->inturb) {
  630. pr_err("fail to allocate int urb");
  631. ret = -ENOMEM;
  632. goto free_readurb;
  633. }
  634. ccid->intbuf = kmalloc(usb_endpoint_maxp(&ep->desc),
  635. GFP_KERNEL);
  636. if (!ccid->intbuf) {
  637. pr_err("fail to allocated int buf");
  638. ret = -ENOMEM;
  639. goto free_inturb;
  640. }
  641. usb_fill_int_urb(ccid->inturb, ccid->udev,
  642. usb_rcvintpipe(ccid->udev, epint_addr),
  643. ccid->intbuf, usb_endpoint_maxp(&ep->desc),
  644. ccid_bridge_int_cb, ccid,
  645. ep->desc.bInterval);
  646. }
  647. if (ccid->int_pipe || device_can_wakeup(&ccid->udev->dev)) {
  648. pr_debug("event support is present");
  649. ccid->events_supported = true;
  650. }
  651. usb_set_intfdata(intf, ccid);
  652. usb_enable_autosuspend(ccid->udev);
  653. mutex_lock(&ccid->open_mutex);
  654. ccid->intf = intf;
  655. wake_up(&ccid->open_wq);
  656. mutex_unlock(&ccid->open_mutex);
  657. pr_info("success");
  658. return 0;
  659. free_inturb:
  660. if (ccid->int_pipe)
  661. usb_free_urb(ccid->inturb);
  662. free_readurb:
  663. usb_free_urb(ccid->readurb);
  664. free_writeurb:
  665. usb_free_urb(ccid->writeurb);
  666. put_udev:
  667. usb_put_dev(ccid->udev);
  668. return ret;
  669. }
  670. static void ccid_bridge_disconnect(struct usb_interface *intf)
  671. {
  672. struct ccid_bridge *ccid = usb_get_intfdata(intf);
  673. pr_debug("called");
  674. usb_kill_urb(ccid->writeurb);
  675. usb_kill_urb(ccid->readurb);
  676. if (ccid->int_pipe)
  677. usb_kill_urb(ccid->inturb);
  678. ccid->event_result = -ENODEV;
  679. wake_up(&ccid->event_wq);
  680. /*
  681. * This would synchronize any ongoing read/write/ioctl.
  682. * After acquiring the mutex, we can safely set
  683. * intf to NULL.
  684. */
  685. mutex_lock(&ccid->open_mutex);
  686. mutex_lock(&ccid->write_mutex);
  687. mutex_lock(&ccid->read_mutex);
  688. mutex_lock(&ccid->event_mutex);
  689. usb_free_urb(ccid->writeurb);
  690. usb_free_urb(ccid->readurb);
  691. if (ccid->int_pipe) {
  692. usb_free_urb(ccid->inturb);
  693. kfree(ccid->intbuf);
  694. ccid->int_pipe = 0;
  695. }
  696. ccid->intf = NULL;
  697. usb_put_dev(ccid->udev);
  698. mutex_unlock(&ccid->event_mutex);
  699. mutex_unlock(&ccid->read_mutex);
  700. mutex_unlock(&ccid->write_mutex);
  701. mutex_unlock(&ccid->open_mutex);
  702. }
  703. static const struct usb_device_id ccid_bridge_ids[] = {
  704. { USB_INTERFACE_INFO(USB_CLASS_CSCID, 0, 0) },
  705. {} /* terminating entry */
  706. };
  707. MODULE_DEVICE_TABLE(usb, ccid_bridge_ids);
  708. static struct usb_driver ccid_bridge_driver = {
  709. .name = "ccid_bridge",
  710. .probe = ccid_bridge_probe,
  711. .disconnect = ccid_bridge_disconnect,
  712. .suspend = ccid_bridge_suspend,
  713. .resume = ccid_bridge_resume,
  714. .id_table = ccid_bridge_ids,
  715. .supports_autosuspend = 1,
  716. };
  717. static int ccid_bridge_stats_show(struct seq_file *s, void *unused)
  718. {
  719. struct ccid_bridge *ccid = s->private;
  720. seq_printf(s, "ccid_bridge: %s\n",
  721. ccid->intf ? "connected" : "disconnected");
  722. seq_printf(s, "ccid_bridge: %s\n",
  723. ccid->opened ? "opened" : "closed");
  724. seq_printf(s, "total writes: %u\n", ccid->n_write);
  725. seq_printf(s, "total reads: %u\n", ccid->n_write);
  726. seq_printf(s, "write/read timeout val: %u\n", ccid_bulk_msg_timeout);
  727. seq_printf(s, "write_timeout: %u\n", ccid->n_write_timeout);
  728. seq_printf(s, "read_timeout: %u\n", ccid->n_read_timeout);
  729. seq_printf(s, "write_max_time (msec): %lu\n", ccid->write_max_time);
  730. seq_printf(s, "read_max_time: (msec): %lu\n", ccid->read_max_time);
  731. return 0;
  732. }
  733. static int ccid_bridge_stats_open(struct inode *inode, struct file *file)
  734. {
  735. return single_open(file, ccid_bridge_stats_show, inode->i_private);
  736. }
  737. static ssize_t ccid_bridge_stats_write(struct file *file,
  738. const char __user *ubuf,
  739. size_t count, loff_t *ppos)
  740. {
  741. struct seq_file *s = file->private_data;
  742. struct ccid_bridge *ccid = s->private;
  743. ccid_bridge_reset_stats(ccid);
  744. return count;
  745. }
  746. const struct file_operations ccid_bridge_stats_ops = {
  747. .open = ccid_bridge_stats_open,
  748. .read = seq_read,
  749. .write = ccid_bridge_stats_write,
  750. .llseek = seq_lseek,
  751. .release = single_release,
  752. };
  753. static int ccid_bridge_debugfs_init(struct ccid_bridge *ccid)
  754. {
  755. struct dentry *dir;
  756. int ret = 0;
  757. dir = debugfs_create_dir("ccid_bridge", NULL);
  758. if (!dir || IS_ERR(dir)) {
  759. ret = -ENODEV;
  760. goto out;
  761. }
  762. ccid->dbg_root = dir;
  763. dir = debugfs_create_file("stats", 0644, ccid->dbg_root, ccid,
  764. &ccid_bridge_stats_ops);
  765. if (!dir) {
  766. debugfs_remove_recursive(ccid->dbg_root);
  767. ccid->dbg_root = NULL;
  768. ret = -ENODEV;
  769. }
  770. out:
  771. return ret;
  772. }
  773. static int __init ccid_bridge_init(void)
  774. {
  775. int ret;
  776. struct ccid_bridge *ccid;
  777. ccid = kzalloc(sizeof(*ccid), GFP_KERNEL);
  778. if (!ccid) {
  779. pr_err("Fail to allocate ccid");
  780. ret = -ENOMEM;
  781. goto out;
  782. }
  783. __ccid_bridge_dev = ccid;
  784. mutex_init(&ccid->open_mutex);
  785. mutex_init(&ccid->write_mutex);
  786. mutex_init(&ccid->read_mutex);
  787. mutex_init(&ccid->event_mutex);
  788. init_waitqueue_head(&ccid->open_wq);
  789. init_waitqueue_head(&ccid->write_wq);
  790. init_waitqueue_head(&ccid->read_wq);
  791. init_waitqueue_head(&ccid->event_wq);
  792. ret = usb_register(&ccid_bridge_driver);
  793. if (ret < 0) {
  794. pr_err("Fail to register ccid usb driver with %d", ret);
  795. goto free_ccid;
  796. }
  797. ret = alloc_chrdev_region(&ccid->chrdev, 0, 1, "ccid_bridge");
  798. if (ret < 0) {
  799. pr_err("Fail to allocate ccid char dev region with %d", ret);
  800. goto unreg_driver;
  801. }
  802. ccid->class = class_create(THIS_MODULE, "ccid_bridge");
  803. if (IS_ERR(ccid->class)) {
  804. ret = PTR_ERR(ccid->class);
  805. pr_err("Fail to create ccid class with %d", ret);
  806. goto unreg_chrdev;
  807. }
  808. cdev_init(&ccid->cdev, &ccid_bridge_fops);
  809. ccid->cdev.owner = THIS_MODULE;
  810. ret = cdev_add(&ccid->cdev, ccid->chrdev, 1);
  811. if (ret < 0) {
  812. pr_err("Fail to add ccid cdev with %d", ret);
  813. goto destroy_class;
  814. }
  815. ccid->device = device_create(ccid->class,
  816. NULL, ccid->chrdev, NULL,
  817. "ccid_bridge");
  818. if (IS_ERR(ccid->device)) {
  819. ret = PTR_ERR(ccid->device);
  820. pr_err("Fail to create ccid device with %d", ret);
  821. goto del_cdev;
  822. }
  823. ccid_bridge_debugfs_init(ccid);
  824. pr_info("success");
  825. return 0;
  826. del_cdev:
  827. cdev_del(&ccid->cdev);
  828. destroy_class:
  829. class_destroy(ccid->class);
  830. unreg_chrdev:
  831. unregister_chrdev_region(ccid->chrdev, 1);
  832. unreg_driver:
  833. usb_deregister(&ccid_bridge_driver);
  834. free_ccid:
  835. mutex_destroy(&ccid->open_mutex);
  836. mutex_destroy(&ccid->write_mutex);
  837. mutex_destroy(&ccid->read_mutex);
  838. mutex_destroy(&ccid->event_mutex);
  839. kfree(ccid);
  840. __ccid_bridge_dev = NULL;
  841. out:
  842. return ret;
  843. }
  844. static void __exit ccid_bridge_exit(void)
  845. {
  846. struct ccid_bridge *ccid = __ccid_bridge_dev;
  847. pr_debug("called");
  848. debugfs_remove_recursive(ccid->dbg_root);
  849. device_destroy(ccid->class, ccid->chrdev);
  850. cdev_del(&ccid->cdev);
  851. class_destroy(ccid->class);
  852. unregister_chrdev_region(ccid->chrdev, 1);
  853. usb_deregister(&ccid_bridge_driver);
  854. mutex_destroy(&ccid->open_mutex);
  855. mutex_destroy(&ccid->write_mutex);
  856. mutex_destroy(&ccid->read_mutex);
  857. mutex_destroy(&ccid->event_mutex);
  858. kfree(ccid);
  859. __ccid_bridge_dev = NULL;
  860. }
  861. module_init(ccid_bridge_init);
  862. module_exit(ccid_bridge_exit);
  863. MODULE_DESCRIPTION("USB CCID bridge driver");
  864. MODULE_LICENSE("GPL v2");