blob: a11f439b30610fe0f32ef544c64fc76b663c0656 [file] [log] [blame]
Chiranjeevi Velempatie130fd02011-11-29 05:06:13 +05301/*
2 * f_ccid.c -- CCID function Driver
3 *
4 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
5
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
9
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
14 */
15
16#include <linux/slab.h>
17#include <linux/kernel.h>
18#include <linux/device.h>
19#include <linux/usb/android_composite.h>
20#include <linux/fs.h>
21#include <linux/usb/ccid_desc.h>
22#include <linux/miscdevice.h>
23
24#include "f_ccid.h"
25
26#define BULK_IN_BUFFER_SIZE sizeof(struct ccid_bulk_in_header)
27#define BULK_OUT_BUFFER_SIZE sizeof(struct ccid_bulk_out_header)
28#define CTRL_BUF_SIZE 4
29#define FUNCTION_NAME "ccid"
30#define CCID_NOTIFY_INTERVAL 5
31#define CCID_NOTIFY_MAXPACKET 4
32
33/* number of tx requests to allocate */
34#define TX_REQ_MAX 4
35
36struct ccid_descs {
37 struct usb_endpoint_descriptor *in;
38 struct usb_endpoint_descriptor *out;
39 struct usb_endpoint_descriptor *notify;
40};
41
42struct ccid_ctrl_dev {
43 atomic_t opened;
44 struct list_head tx_q;
45 wait_queue_head_t tx_wait_q;
46 unsigned char buf[CTRL_BUF_SIZE];
47 int tx_ctrl_done;
48};
49
50struct ccid_bulk_dev {
51 atomic_t error;
52 atomic_t opened;
53 atomic_t rx_req_busy;
54 wait_queue_head_t read_wq;
55 wait_queue_head_t write_wq;
56 struct usb_request *rx_req;
57 int rx_done;
58 struct list_head tx_idle;
59};
60
61struct f_ccid {
62 struct usb_function function;
63 struct usb_composite_dev *cdev;
64 int ifc_id;
65 spinlock_t lock;
66 atomic_t online;
67 /* usb descriptors */
68 struct ccid_descs fs;
69 struct ccid_descs hs;
70 /* usb eps*/
71 struct usb_ep *notify;
72 struct usb_ep *in;
73 struct usb_ep *out;
74 struct usb_endpoint_descriptor *in_desc;
75 struct usb_endpoint_descriptor *out_desc;
76 struct usb_endpoint_descriptor *notify_desc;
77 struct usb_request *notify_req;
78 struct ccid_ctrl_dev ctrl_dev;
79 struct ccid_bulk_dev bulk_dev;
80 int dtr_state;
81};
82
83static struct f_ccid *_ccid_dev;
84static struct miscdevice ccid_bulk_device;
85static struct miscdevice ccid_ctrl_device;
86
87/* Interface Descriptor: */
88static struct usb_interface_descriptor ccid_interface_desc = {
89 .bLength = USB_DT_INTERFACE_SIZE,
90 .bDescriptorType = USB_DT_INTERFACE,
91 .bNumEndpoints = 3,
92 .bInterfaceClass = USB_CLASS_CSCID,
93 .bInterfaceSubClass = 0,
94 .bInterfaceProtocol = 0,
95};
96/* CCID Class Descriptor */
97static struct usb_ccid_class_descriptor ccid_class_desc = {
98 .bLength = sizeof(ccid_class_desc),
99 .bDescriptorType = CCID_DECRIPTOR_TYPE,
100 .bcdCCID = CCID1_10,
101 .bMaxSlotIndex = 0,
102 /* This value indicates what voltages the CCID can supply to slots */
103 .bVoltageSupport = VOLTS_3_0,
104 .dwProtocols = PROTOCOL_TO,
105 /* Default ICC clock frequency in KHz */
106 .dwDefaultClock = 3580,
107 /* Maximum supported ICC clock frequency in KHz */
108 .dwMaximumClock = 3580,
109 .bNumClockSupported = 0,
110 /* Default ICC I/O data rate in bps */
111 .dwDataRate = 9600,
112 /* Maximum supported ICC I/O data rate in bps */
113 .dwMaxDataRate = 9600,
114 .bNumDataRatesSupported = 0,
115 .dwMaxIFSD = 0,
116 .dwSynchProtocols = 0,
117 .dwMechanical = 0,
118 /* This value indicates what intelligent features the CCID has */
119 .dwFeatures = CCID_FEATURES_EXC_SAPDU |
120 CCID_FEATURES_AUTO_PNEGO |
121 CCID_FEATURES_AUTO_BAUD |
122 CCID_FEATURES_AUTO_CLOCK |
123 CCID_FEATURES_AUTO_VOLT |
124 CCID_FEATURES_AUTO_ACTIV |
125 CCID_FEATURES_AUTO_PCONF,
126 /* extended APDU level Message Length */
127 .dwMaxCCIDMessageLength = 0x200,
128 .bClassGetResponse = 0x0,
129 .bClassEnvelope = 0x0,
130 .wLcdLayout = 0,
131 .bPINSupport = 0,
132 .bMaxCCIDBusySlots = 1
133};
134/* Full speed support: */
135static struct usb_endpoint_descriptor ccid_fs_notify_desc = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
138 .bEndpointAddress = USB_DIR_IN,
139 .bmAttributes = USB_ENDPOINT_XFER_INT,
140 .wMaxPacketSize = __constant_cpu_to_le16(CCID_NOTIFY_MAXPACKET),
141 .bInterval = 1 << CCID_NOTIFY_INTERVAL,
142};
143
144static struct usb_endpoint_descriptor ccid_fs_in_desc = {
145 .bLength = USB_DT_ENDPOINT_SIZE,
146 .bDescriptorType = USB_DT_ENDPOINT,
147 .bEndpointAddress = USB_DIR_IN,
148 .bmAttributes = USB_ENDPOINT_XFER_BULK,
149 .wMaxPacketSize = __constant_cpu_to_le16(64),
150};
151
152static struct usb_endpoint_descriptor ccid_fs_out_desc = {
153 .bLength = USB_DT_ENDPOINT_SIZE,
154 .bDescriptorType = USB_DT_ENDPOINT,
155 .bEndpointAddress = USB_DIR_OUT,
156 .bmAttributes = USB_ENDPOINT_XFER_BULK,
157 .wMaxPacketSize = __constant_cpu_to_le16(64),
158};
159
160static struct usb_descriptor_header *ccid_fs_descs[] = {
161 (struct usb_descriptor_header *) &ccid_interface_desc,
162 (struct usb_descriptor_header *) &ccid_class_desc,
163 (struct usb_descriptor_header *) &ccid_fs_notify_desc,
164 (struct usb_descriptor_header *) &ccid_fs_in_desc,
165 (struct usb_descriptor_header *) &ccid_fs_out_desc,
166 NULL,
167};
168
169/* High speed support: */
170static struct usb_endpoint_descriptor ccid_hs_notify_desc = {
171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
173 .bEndpointAddress = USB_DIR_IN,
174 .bmAttributes = USB_ENDPOINT_XFER_INT,
175 .wMaxPacketSize = __constant_cpu_to_le16(CCID_NOTIFY_MAXPACKET),
176 .bInterval = CCID_NOTIFY_INTERVAL + 4,
177};
178
179static struct usb_endpoint_descriptor ccid_hs_in_desc = {
180 .bLength = USB_DT_ENDPOINT_SIZE,
181 .bDescriptorType = USB_DT_ENDPOINT,
182 .bEndpointAddress = USB_DIR_IN,
183 .bmAttributes = USB_ENDPOINT_XFER_BULK,
184 .wMaxPacketSize = __constant_cpu_to_le16(512),
185};
186
187static struct usb_endpoint_descriptor ccid_hs_out_desc = {
188 .bLength = USB_DT_ENDPOINT_SIZE,
189 .bDescriptorType = USB_DT_ENDPOINT,
190 .bEndpointAddress = USB_DIR_OUT,
191 .bmAttributes = USB_ENDPOINT_XFER_BULK,
192 .wMaxPacketSize = __constant_cpu_to_le16(512),
193};
194
195static struct usb_descriptor_header *ccid_hs_descs[] = {
196 (struct usb_descriptor_header *) &ccid_interface_desc,
197 (struct usb_descriptor_header *) &ccid_class_desc,
198 (struct usb_descriptor_header *) &ccid_hs_notify_desc,
199 (struct usb_descriptor_header *) &ccid_hs_in_desc,
200 (struct usb_descriptor_header *) &ccid_hs_out_desc,
201 NULL,
202};
203
204static inline struct f_ccid *func_to_ccid(struct usb_function *f)
205{
206 return container_of(f, struct f_ccid, function);
207}
208
209static void ccid_req_put(struct f_ccid *ccid_dev, struct list_head *head,
210 struct usb_request *req)
211{
212 unsigned long flags;
213
214 spin_lock_irqsave(&ccid_dev->lock, flags);
215 list_add_tail(&req->list, head);
216 spin_unlock_irqrestore(&ccid_dev->lock, flags);
217}
218
219static struct usb_request *ccid_req_get(struct f_ccid *ccid_dev,
220 struct list_head *head)
221{
222 unsigned long flags;
223 struct usb_request *req = NULL;
224
225 spin_lock_irqsave(&ccid_dev->lock, flags);
226 if (!list_empty(head)) {
227 req = list_first_entry(head, struct usb_request, list);
228 list_del(&req->list);
229 }
230 spin_unlock_irqrestore(&ccid_dev->lock, flags);
231 return req;
232}
233
234static void ccid_notify_complete(struct usb_ep *ep, struct usb_request *req)
235{
236 switch (req->status) {
237 case -ECONNRESET:
238 case -ESHUTDOWN:
239 case 0:
240 break;
241 default:
242 pr_err("CCID notify ep error %d\n", req->status);
243 }
244}
245
246static void ccid_bulk_complete_in(struct usb_ep *ep, struct usb_request *req)
247{
248 struct f_ccid *ccid_dev = _ccid_dev;
249 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
250
251 if (req->status != 0)
252 atomic_set(&bulk_dev->error, 1);
253
254 ccid_req_put(ccid_dev, &bulk_dev->tx_idle, req);
255 wake_up(&bulk_dev->write_wq);
256}
257
258static void ccid_bulk_complete_out(struct usb_ep *ep, struct usb_request *req)
259{
260 struct f_ccid *ccid_dev = _ccid_dev;
261 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
262 if (req->status != 0)
263 atomic_set(&bulk_dev->error, 1);
264
265 bulk_dev->rx_done = 1;
266 wake_up(&bulk_dev->read_wq);
267}
268
269static struct usb_request *
270ccid_request_alloc(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
271{
272 struct usb_request *req;
273
274 req = usb_ep_alloc_request(ep, kmalloc_flags);
275
276 if (req != NULL) {
277 req->length = len;
278 req->buf = kmalloc(len, kmalloc_flags);
279 if (req->buf == NULL) {
280 usb_ep_free_request(ep, req);
281 req = NULL;
282 }
283 }
284
285 return req ? req : ERR_PTR(-ENOMEM);
286}
287
288static void ccid_request_free(struct usb_request *req, struct usb_ep *ep)
289{
290 if (req) {
291 kfree(req->buf);
292 usb_ep_free_request(ep, req);
293 }
294}
295
296static int
297ccid_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
298{
299 struct f_ccid *ccid_dev = container_of(f, struct f_ccid, function);
300 struct ccid_ctrl_dev *ctrl_dev = &ccid_dev->ctrl_dev;
301 struct usb_composite_dev *cdev = f->config->cdev;
302 struct usb_request *req = cdev->req;
303 int ret = -EOPNOTSUPP;
304 u16 w_index = le16_to_cpu(ctrl->wIndex);
305 u16 w_value = le16_to_cpu(ctrl->wValue);
306 u16 w_length = le16_to_cpu(ctrl->wLength);
307
308 if (!atomic_read(&ccid_dev->online))
309 return -ENOTCONN;
310
311 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
312
313 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
314 | CCIDGENERICREQ_ABORT:
315 if (w_length != 0)
316 goto invalid;
317 ctrl_dev->buf[0] = CCIDGENERICREQ_ABORT;
318 ctrl_dev->buf[1] = w_value & 0xFF;
319 ctrl_dev->buf[2] = (w_value >> 8) & 0xFF;
320 ctrl_dev->buf[3] = 0x00;
321 ctrl_dev->tx_ctrl_done = 1;
322 wake_up(&ctrl_dev->tx_wait_q);
323 return 0;
324
325 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
326 | CCIDGENERICREQ_GET_CLOCK_FREQUENCIES:
327 if (w_length > req->length)
328 goto invalid;
329 *(u32 *) req->buf =
330 cpu_to_le32(ccid_class_desc.dwDefaultClock);
331 ret = min_t(u32, w_length,
332 sizeof(ccid_class_desc.dwDefaultClock));
333 break;
334
335 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
336 | CCIDGENERICREQ_GET_DATA_RATES:
337 if (w_length > req->length)
338 goto invalid;
339 *(u32 *) req->buf = cpu_to_le32(ccid_class_desc.dwDataRate);
340 ret = min_t(u32, w_length, sizeof(ccid_class_desc.dwDataRate));
341 break;
342
343 default:
344invalid:
345 pr_debug("invalid control req%02x.%02x v%04x i%04x l%d\n",
346 ctrl->bRequestType, ctrl->bRequest,
347 w_value, w_index, w_length);
348 }
349
350 /* respond with data transfer or status phase? */
351 if (ret >= 0) {
352 pr_debug("ccid req%02x.%02x v%04x i%04x l%d\n",
353 ctrl->bRequestType, ctrl->bRequest,
354 w_value, w_index, w_length);
355 req->length = ret;
356 ret = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
357 if (ret < 0)
358 pr_err("ccid ep0 enqueue err %d\n", ret);
359 }
360
361 return ret;
362}
363
364static void ccid_function_disable(struct usb_function *f)
365{
366 struct f_ccid *ccid_dev = func_to_ccid(f);
367 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
368 struct ccid_ctrl_dev *ctrl_dev = &ccid_dev->ctrl_dev;
369 struct usb_request *req;
370
371 /* Disable endpoints */
372 usb_ep_disable(ccid_dev->notify);
373 usb_ep_disable(ccid_dev->in);
374 usb_ep_disable(ccid_dev->out);
375 /* Free endpoint related requests */
376 ccid_request_free(ccid_dev->notify_req, ccid_dev->notify);
377 if (!atomic_read(&bulk_dev->rx_req_busy))
378 ccid_request_free(bulk_dev->rx_req, ccid_dev->out);
379 while ((req = ccid_req_get(ccid_dev, &bulk_dev->tx_idle)))
380 ccid_request_free(req, ccid_dev->in);
381
382 ccid_dev->dtr_state = 0;
383 atomic_set(&ccid_dev->online, 0);
384 /* Wake up threads */
385 wake_up(&bulk_dev->write_wq);
386 wake_up(&bulk_dev->read_wq);
387 wake_up(&ctrl_dev->tx_wait_q);
388
389}
390
391static int
392ccid_function_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
393{
394 struct f_ccid *ccid_dev = func_to_ccid(f);
395 struct usb_composite_dev *cdev = ccid_dev->cdev;
396 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
397 struct usb_request *req;
398 int ret = 0;
399 int i;
400
401 ccid_dev->notify_req = ccid_request_alloc(ccid_dev->notify,
402 sizeof(struct usb_ccid_notification), GFP_ATOMIC);
403 if (IS_ERR(ccid_dev->notify_req)) {
404 pr_err("%s: unable to allocate memory for notify req\n",
405 __func__);
406 return PTR_ERR(ccid_dev->notify_req);
407 }
408 ccid_dev->notify_req->complete = ccid_notify_complete;
409 ccid_dev->notify_req->context = ccid_dev;
410
411 /* now allocate requests for our endpoints */
412 req = ccid_request_alloc(ccid_dev->out, BULK_OUT_BUFFER_SIZE,
413 GFP_ATOMIC);
414 if (IS_ERR(req)) {
415 pr_err("%s: unable to allocate memory for out req\n",
416 __func__);
417 ret = PTR_ERR(req);
418 goto free_notify;
419 }
420 req->complete = ccid_bulk_complete_out;
421 req->context = ccid_dev;
422 bulk_dev->rx_req = req;
423
424 for (i = 0; i < TX_REQ_MAX; i++) {
425 req = ccid_request_alloc(ccid_dev->in, BULK_IN_BUFFER_SIZE,
426 GFP_ATOMIC);
427 if (IS_ERR(req)) {
428 pr_err("%s: unable to allocate memory for in req\n",
429 __func__);
430 ret = PTR_ERR(req);
431 goto free_bulk_out;
432 }
433 req->complete = ccid_bulk_complete_in;
434 req->context = ccid_dev;
435 ccid_req_put(ccid_dev, &bulk_dev->tx_idle, req);
436 }
437
438 /* choose the descriptors and enable endpoints */
439 ccid_dev->notify_desc = ep_choose(cdev->gadget,
440 ccid_dev->hs.notify,
441 ccid_dev->fs.notify);
442 ret = usb_ep_enable(ccid_dev->notify, ccid_dev->notify_desc);
443 if (ret) {
444 pr_err("%s: usb ep#%s enable failed, err#%d\n",
445 __func__, ccid_dev->notify->name, ret);
446 goto free_bulk_in;
447 }
448 ccid_dev->notify->driver_data = ccid_dev;
449
450 ccid_dev->in_desc = ep_choose(cdev->gadget,
451 ccid_dev->hs.in, ccid_dev->fs.in);
452 ret = usb_ep_enable(ccid_dev->in, ccid_dev->in_desc);
453 if (ret) {
454 pr_err("%s: usb ep#%s enable failed, err#%d\n",
455 __func__, ccid_dev->in->name, ret);
456 goto disable_ep_notify;
457 }
458
459 ccid_dev->out_desc = ep_choose(cdev->gadget,
460 ccid_dev->hs.out, ccid_dev->fs.out);
461 ret = usb_ep_enable(ccid_dev->out, ccid_dev->out_desc);
462 if (ret) {
463 pr_err("%s: usb ep#%s enable failed, err#%d\n",
464 __func__, ccid_dev->out->name, ret);
465 goto disable_ep_in;
466 }
467 ccid_dev->dtr_state = 1;
468 atomic_set(&ccid_dev->online, 1);
469 return ret;
470
471disable_ep_in:
472 usb_ep_disable(ccid_dev->in);
473disable_ep_notify:
474 usb_ep_disable(ccid_dev->notify);
475 ccid_dev->notify->driver_data = NULL;
476free_bulk_in:
477 while ((req = ccid_req_get(ccid_dev, &bulk_dev->tx_idle)))
478 ccid_request_free(req, ccid_dev->in);
479free_bulk_out:
480 ccid_request_free(bulk_dev->rx_req, ccid_dev->out);
481free_notify:
482 ccid_request_free(ccid_dev->notify_req, ccid_dev->notify);
483 return ret;
484}
485
486static void ccid_function_unbind(struct usb_configuration *c,
487 struct usb_function *f)
488{
489 if (gadget_is_dualspeed(c->cdev->gadget))
490 usb_free_descriptors(f->hs_descriptors);
491 usb_free_descriptors(f->descriptors);
492
493}
494
495static int ccid_function_bind(struct usb_configuration *c,
496 struct usb_function *f)
497{
498 struct f_ccid *ccid_dev = func_to_ccid(f);
499 struct usb_ep *ep;
500 struct usb_composite_dev *cdev = c->cdev;
501 int ret = -ENODEV;
502
503 ccid_dev->ifc_id = usb_interface_id(c, f);
504 if (ccid_dev->ifc_id < 0) {
505 pr_err("%s: unable to allocate ifc id, err:%d",
506 __func__, ccid_dev->ifc_id);
507 return ccid_dev->ifc_id;
508 }
509 ccid_interface_desc.bInterfaceNumber = ccid_dev->ifc_id;
510
511 ep = usb_ep_autoconfig(cdev->gadget, &ccid_fs_notify_desc);
512 if (!ep) {
513 pr_err("%s: usb epnotify autoconfig failed\n", __func__);
514 return -ENODEV;
515 }
516 ccid_dev->notify = ep;
517 ep->driver_data = cdev;
518
519 ep = usb_ep_autoconfig(cdev->gadget, &ccid_fs_in_desc);
520 if (!ep) {
521 pr_err("%s: usb epin autoconfig failed\n", __func__);
522 ret = -ENODEV;
523 goto ep_auto_in_fail;
524 }
525 ccid_dev->in = ep;
526 ep->driver_data = cdev;
527
528 ep = usb_ep_autoconfig(cdev->gadget, &ccid_fs_out_desc);
529 if (!ep) {
530 pr_err("%s: usb epout autoconfig failed\n", __func__);
531 ret = -ENODEV;
532 goto ep_auto_out_fail;
533 }
534 ccid_dev->out = ep;
535 ep->driver_data = cdev;
536
537 f->descriptors = usb_copy_descriptors(ccid_fs_descs);
538 if (!f->descriptors)
539 goto ep_auto_out_fail;
540
541 ccid_dev->fs.in = usb_find_endpoint(ccid_fs_descs,
542 f->descriptors,
543 &ccid_fs_in_desc);
544 ccid_dev->fs.out = usb_find_endpoint(ccid_fs_descs,
545 f->descriptors,
546 &ccid_fs_out_desc);
547 ccid_dev->fs.notify = usb_find_endpoint(ccid_fs_descs,
548 f->descriptors,
549 &ccid_fs_notify_desc);
550
551 if (gadget_is_dualspeed(cdev->gadget)) {
552 ccid_hs_in_desc.bEndpointAddress =
553 ccid_fs_in_desc.bEndpointAddress;
554 ccid_hs_out_desc.bEndpointAddress =
555 ccid_fs_out_desc.bEndpointAddress;
556 ccid_hs_notify_desc.bEndpointAddress =
557 ccid_fs_notify_desc.bEndpointAddress;
558
559 /* copy descriptors, and track endpoint copies */
560 f->hs_descriptors = usb_copy_descriptors(ccid_hs_descs);
561 if (!f->hs_descriptors)
562 goto ep_auto_out_fail;
563
564 ccid_dev->hs.in = usb_find_endpoint(ccid_hs_descs,
565 f->hs_descriptors, &ccid_hs_in_desc);
566 ccid_dev->hs.out = usb_find_endpoint(ccid_hs_descs,
567 f->hs_descriptors, &ccid_hs_out_desc);
568 ccid_dev->hs.notify = usb_find_endpoint(ccid_hs_descs,
569 f->hs_descriptors, &ccid_hs_notify_desc);
570 }
571
572 pr_debug("%s: CCID %s Speed, IN:%s OUT:%s\n", __func__,
573 gadget_is_dualspeed(cdev->gadget) ? "dual" : "full",
574 ccid_dev->in->name, ccid_dev->out->name);
575
576 return 0;
577
578ep_auto_out_fail:
579 ccid_dev->out->driver_data = NULL;
580 ccid_dev->out = NULL;
581ep_auto_in_fail:
582 ccid_dev->in->driver_data = NULL;
583 ccid_dev->in = NULL;
584
585 return ret;
586}
587
588static int ccid_bulk_open(struct inode *ip, struct file *fp)
589{
590 struct f_ccid *ccid_dev = _ccid_dev;
591 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
592 unsigned long flags;
593
594 pr_debug("ccid_bulk_open\n");
595 if (!atomic_read(&ccid_dev->online)) {
596 pr_debug("%s: USB cable not connected\n", __func__);
597 return -ENODEV;
598 }
599
600 if (atomic_read(&bulk_dev->opened)) {
601 pr_debug("%s: bulk device is already opened\n", __func__);
602 return -EBUSY;
603 }
604 atomic_set(&bulk_dev->opened, 1);
605 /* clear the error latch */
606 atomic_set(&bulk_dev->error, 0);
607 spin_lock_irqsave(&ccid_dev->lock, flags);
608 fp->private_data = ccid_dev;
609 spin_unlock_irqrestore(&ccid_dev->lock, flags);
610
611 return 0;
612}
613
614static int ccid_bulk_release(struct inode *ip, struct file *fp)
615{
616 struct f_ccid *ccid_dev = fp->private_data;
617 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
618
619 pr_debug("ccid_bulk_release\n");
620 atomic_set(&bulk_dev->opened, 0);
621 return 0;
622}
623
624static ssize_t ccid_bulk_read(struct file *fp, char __user *buf,
625 size_t count, loff_t *pos)
626{
627 struct f_ccid *ccid_dev = fp->private_data;
628 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
629 struct usb_request *req;
630 int r = count, xfer;
631 int ret;
632 unsigned long flags;
633
634 pr_debug("ccid_bulk_read(%d)\n", count);
635
636 if (count > BULK_OUT_BUFFER_SIZE) {
637 pr_err("%s: max_buffer_size:%d given_pkt_size:%d\n",
638 __func__, BULK_OUT_BUFFER_SIZE, count);
639 return -ENOMEM;
640 }
641
642 if (atomic_read(&bulk_dev->error)) {
643 r = -EIO;
644 pr_err("%s bulk_dev_error\n", __func__);
645 goto done;
646 }
647
648requeue_req:
649 spin_lock_irqsave(&ccid_dev->lock, flags);
650 if (!atomic_read(&ccid_dev->online)) {
651 pr_debug("%s: USB cable not connected\n", __func__);
652 return -ENODEV;
653 }
654 /* queue a request */
655 req = bulk_dev->rx_req;
656 req->length = count;
657 bulk_dev->rx_done = 0;
658 spin_unlock_irqrestore(&ccid_dev->lock, flags);
659 ret = usb_ep_queue(ccid_dev->out, req, GFP_KERNEL);
660 if (ret < 0) {
661 r = -EIO;
662 pr_err("%s usb ep queue failed\n", __func__);
663 atomic_set(&bulk_dev->error, 1);
664 goto done;
665 }
666 /* wait for a request to complete */
667 ret = wait_event_interruptible(bulk_dev->read_wq, bulk_dev->rx_done ||
668 atomic_read(&bulk_dev->error) ||
669 !atomic_read(&ccid_dev->online));
670 if (ret < 0) {
671 atomic_set(&bulk_dev->error, 1);
672 r = ret;
673 usb_ep_dequeue(ccid_dev->out, req);
674 goto done;
675 }
676 if (!atomic_read(&bulk_dev->error)) {
677 spin_lock_irqsave(&ccid_dev->lock, flags);
678 if (!atomic_read(&ccid_dev->online)) {
679 spin_unlock_irqrestore(&ccid_dev->lock, flags);
680 pr_debug("%s: USB cable not connected\n", __func__);
681 r = -ENODEV;
682 goto done;
683 }
684 /* If we got a 0-len packet, throw it back and try again. */
685 if (req->actual == 0) {
686 spin_unlock_irqrestore(&ccid_dev->lock, flags);
687 goto requeue_req;
688 }
689 xfer = (req->actual < count) ? req->actual : count;
690 atomic_set(&bulk_dev->rx_req_busy, 1);
691 spin_unlock_irqrestore(&ccid_dev->lock, flags);
692
693 if (copy_to_user(buf, req->buf, xfer))
694 r = -EFAULT;
695
696 spin_lock_irqsave(&ccid_dev->lock, flags);
697 atomic_set(&bulk_dev->rx_req_busy, 0);
698 if (!atomic_read(&ccid_dev->online)) {
699 ccid_request_free(bulk_dev->rx_req, ccid_dev->out);
700 spin_unlock_irqrestore(&ccid_dev->lock, flags);
701 pr_debug("%s: USB cable not connected\n", __func__);
702 r = -ENODEV;
703 goto done;
704 }
705 spin_unlock_irqrestore(&ccid_dev->lock, flags);
706 } else {
707 r = -EIO;
708 }
709done:
710 pr_debug("ccid_bulk_read returning %d\n", r);
711 return r;
712}
713
714static ssize_t ccid_bulk_write(struct file *fp, const char __user *buf,
715 size_t count, loff_t *pos)
716{
717 struct f_ccid *ccid_dev = fp->private_data;
718 struct ccid_bulk_dev *bulk_dev = &ccid_dev->bulk_dev;
719 struct usb_request *req = 0;
720 int r = count;
721 int ret;
722 unsigned long flags;
723
724 pr_debug("ccid_bulk_write(%d)\n", count);
725
726 if (!atomic_read(&ccid_dev->online)) {
727 pr_debug("%s: USB cable not connected\n", __func__);
728 return -ENODEV;
729 }
730
731 if (!count) {
732 pr_err("%s: zero length ctrl pkt\n", __func__);
733 return -ENODEV;
734 }
735 if (count > BULK_IN_BUFFER_SIZE) {
736 pr_err("%s: max_buffer_size:%d given_pkt_size:%d\n",
737 __func__, BULK_IN_BUFFER_SIZE, count);
738 return -ENOMEM;
739 }
740
741
742 /* get an idle tx request to use */
743 ret = wait_event_interruptible(bulk_dev->write_wq,
744 ((req = ccid_req_get(ccid_dev, &bulk_dev->tx_idle)) ||
745 atomic_read(&bulk_dev->error)));
746
747 if (ret < 0) {
748 r = ret;
749 goto done;
750 }
751
752 if (atomic_read(&bulk_dev->error)) {
753 pr_err(" %s dev->error\n", __func__);
754 r = -EIO;
755 goto done;
756 }
757 if (copy_from_user(req->buf, buf, count)) {
758 if (!atomic_read(&ccid_dev->online)) {
759 pr_debug("%s: USB cable not connected\n",
760 __func__);
761 ccid_request_free(req, ccid_dev->in);
762 r = -ENODEV;
763 } else {
764 ccid_req_put(ccid_dev, &bulk_dev->tx_idle, req);
765 r = -EFAULT;
766 }
767 goto done;
768 }
769 req->length = count;
770 ret = usb_ep_queue(ccid_dev->in, req, GFP_KERNEL);
771 if (ret < 0) {
772 pr_debug("ccid_bulk_write: xfer error %d\n", ret);
773 atomic_set(&bulk_dev->error, 1);
774 ccid_req_put(ccid_dev, &bulk_dev->tx_idle, req);
775 r = -EIO;
776 spin_lock_irqsave(&ccid_dev->lock, flags);
777 if (!atomic_read(&ccid_dev->online)) {
778 spin_unlock_irqrestore(&ccid_dev->lock, flags);
779 pr_debug("%s: USB cable not connected\n",
780 __func__);
781 while ((req = ccid_req_get(ccid_dev,
782 &bulk_dev->tx_idle)))
783 ccid_request_free(req, ccid_dev->in);
784 r = -ENODEV;
785 }
786 spin_unlock_irqrestore(&ccid_dev->lock, flags);
787 goto done;
788 }
789done:
790 pr_debug("ccid_bulk_write returning %d\n", r);
791 return r;
792}
793
794static const struct file_operations ccid_bulk_fops = {
795 .owner = THIS_MODULE,
796 .read = ccid_bulk_read,
797 .write = ccid_bulk_write,
798 .open = ccid_bulk_open,
799 .release = ccid_bulk_release,
800};
801
802static struct miscdevice ccid_bulk_device = {
803 .minor = MISC_DYNAMIC_MINOR,
804 .name = "ccid_bulk",
805 .fops = &ccid_bulk_fops,
806};
807
808static int ccid_bulk_device_init(struct f_ccid *dev)
809{
810 int ret;
811 struct ccid_bulk_dev *bulk_dev = &dev->bulk_dev;
812
813 init_waitqueue_head(&bulk_dev->read_wq);
814 init_waitqueue_head(&bulk_dev->write_wq);
815 INIT_LIST_HEAD(&bulk_dev->tx_idle);
816
817 ret = misc_register(&ccid_bulk_device);
818 if (ret) {
819 pr_err("%s: failed to register misc device\n", __func__);
820 return ret;
821 }
822
823 return 0;
824}
825
826static int ccid_ctrl_open(struct inode *inode, struct file *fp)
827{
828 struct f_ccid *ccid_dev = _ccid_dev;
829 struct ccid_ctrl_dev *ctrl_dev = &ccid_dev->ctrl_dev;
830 unsigned long flags;
831
832 if (!atomic_read(&ccid_dev->online)) {
833 pr_debug("%s: USB cable not connected\n", __func__);
834 return -ENODEV;
835 }
836 if (atomic_read(&ctrl_dev->opened)) {
837 pr_debug("%s: ctrl device is already opened\n", __func__);
838 return -EBUSY;
839 }
840 atomic_set(&ctrl_dev->opened, 1);
841 spin_lock_irqsave(&ccid_dev->lock, flags);
842 fp->private_data = ccid_dev;
843 spin_unlock_irqrestore(&ccid_dev->lock, flags);
844
845 return 0;
846}
847
848
849static int ccid_ctrl_release(struct inode *inode, struct file *fp)
850{
851 struct f_ccid *ccid_dev = fp->private_data;
852 struct ccid_ctrl_dev *ctrl_dev = &ccid_dev->ctrl_dev;
853
854 atomic_set(&ctrl_dev->opened, 0);
855
856 return 0;
857}
858
859static ssize_t ccid_ctrl_read(struct file *fp, char __user *buf,
860 size_t count, loff_t *ppos)
861{
862 struct f_ccid *ccid_dev = fp->private_data;
863 struct ccid_ctrl_dev *ctrl_dev = &ccid_dev->ctrl_dev;
864 int ret = 0;
865
866 if (!atomic_read(&ccid_dev->online)) {
867 pr_debug("%s: USB cable not connected\n", __func__);
868 return -ENODEV;
869 }
870 if (count > CTRL_BUF_SIZE)
871 count = CTRL_BUF_SIZE;
872
873 ret = wait_event_interruptible(ctrl_dev->tx_wait_q,
874 ctrl_dev->tx_ctrl_done);
875 if (ret < 0)
876 return ret;
877 ctrl_dev->tx_ctrl_done = 0;
878
879 if (!atomic_read(&ccid_dev->online)) {
880 pr_debug("%s: USB cable not connected\n", __func__);
881 return -ENODEV;
882 }
883 ret = copy_to_user(buf, ctrl_dev->buf, count);
884 if (ret)
885 return -EFAULT;
886
887 return count;
888}
889
890static long
891ccid_ctrl_ioctl(struct file *fp, unsigned cmd, u_long arg)
892{
893 struct f_ccid *ccid_dev = fp->private_data;
894 struct usb_request *req = ccid_dev->notify_req;
895 struct usb_ccid_notification *ccid_notify = req->buf;
896 void __user *argp = (void __user *)arg;
897 int ret = 0;
898
899 switch (cmd) {
900 case CCID_NOTIFY_CARD:
901 if (copy_from_user(ccid_notify, argp,
902 sizeof(struct usb_ccid_notification)))
903 return -EFAULT;
904 req->length = 2;
905 break;
906 case CCID_NOTIFY_HWERROR:
907 if (copy_from_user(ccid_notify, argp,
908 sizeof(struct usb_ccid_notification)))
909 return -EFAULT;
910 req->length = 4;
911 break;
912 case CCID_READ_DTR:
913 if (copy_to_user((int *)arg, &ccid_dev->dtr_state, sizeof(int)))
914 return -EFAULT;
915 return 0;
916 }
917 ret = usb_ep_queue(ccid_dev->notify, ccid_dev->notify_req, GFP_KERNEL);
918 if (ret < 0) {
919 pr_err("ccid notify ep enqueue error %d\n", ret);
920 return ret;
921 }
922 return 0;
923}
924
925static const struct file_operations ccid_ctrl_fops = {
926 .owner = THIS_MODULE,
927 .open = ccid_ctrl_open,
928 .release = ccid_ctrl_release,
929 .read = ccid_ctrl_read,
930 .unlocked_ioctl = ccid_ctrl_ioctl,
931};
932
933static struct miscdevice ccid_ctrl_device = {
934 .minor = MISC_DYNAMIC_MINOR,
935 .name = "ccid_ctrl",
936 .fops = &ccid_ctrl_fops,
937};
938
939static int ccid_ctrl_device_init(struct f_ccid *dev)
940{
941 int ret;
942 struct ccid_ctrl_dev *ctrl_dev = &dev->ctrl_dev;
943
944 INIT_LIST_HEAD(&ctrl_dev->tx_q);
945 init_waitqueue_head(&ctrl_dev->tx_wait_q);
946
947 ret = misc_register(&ccid_ctrl_device);
948 if (ret) {
949 pr_err("%s: failed to register misc device\n", __func__);
950 return ret;
951 }
952
953 return 0;
954}
955
956static int ccid_bind_config(struct usb_configuration *c)
957{
958 struct f_ccid *ccid_dev = _ccid_dev;
959
960 pr_debug("ccid_bind_config\n");
961 ccid_dev->cdev = c->cdev;
962 ccid_dev->function.name = FUNCTION_NAME;
963 ccid_dev->function.descriptors = ccid_fs_descs;
964 ccid_dev->function.hs_descriptors = ccid_hs_descs;
965 ccid_dev->function.bind = ccid_function_bind;
966 ccid_dev->function.unbind = ccid_function_unbind;
967 ccid_dev->function.set_alt = ccid_function_set_alt;
968 ccid_dev->function.setup = ccid_function_setup;
969 ccid_dev->function.disable = ccid_function_disable;
970
971 return usb_add_function(c, &ccid_dev->function);
972
973}
974
975static int ccid_setup(void)
976{
977 struct f_ccid *ccid_dev;
978 int ret;
979
980 ccid_dev = kzalloc(sizeof(*ccid_dev), GFP_KERNEL);
981 if (!ccid_dev)
982 return -ENOMEM;
983
984 _ccid_dev = ccid_dev;
985 spin_lock_init(&ccid_dev->lock);
986
987 ret = ccid_ctrl_device_init(ccid_dev);
988 if (ret) {
989 pr_err("%s: ccid_ctrl_device_init failed, err:%d\n",
990 __func__, ret);
991 goto err_ctrl_init;
992 }
993 ret = ccid_bulk_device_init(ccid_dev);
994 if (ret) {
995 pr_err("%s: ccid_bulk_device_init failed, err:%d\n",
996 __func__, ret);
997 goto err_bulk_init;
998 }
999
1000 return 0;
1001err_bulk_init:
1002 misc_deregister(&ccid_ctrl_device);
1003err_ctrl_init:
1004 kfree(ccid_dev);
1005 pr_err("ccid gadget driver failed to initialize\n");
1006 return ret;
1007}
1008
1009static void ccid_cleanup(void)
1010{
1011 misc_deregister(&ccid_bulk_device);
1012 misc_deregister(&ccid_ctrl_device);
1013 kfree(_ccid_dev);
1014}