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