Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kref.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/usb.h> |
| 23 | #include <mach/diag_bridge.h> |
| 24 | |
| 25 | #define DRIVER_DESC "USB host diag bridge driver" |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 26 | #define DRIVER_VERSION "1.0" |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 27 | |
| 28 | struct diag_bridge { |
| 29 | struct usb_device *udev; |
| 30 | struct usb_interface *ifc; |
| 31 | struct usb_anchor submitted; |
| 32 | __u8 in_epAddr; |
| 33 | __u8 out_epAddr; |
Jack Pham | 3ae820f | 2011-12-14 16:21:04 -0800 | [diff] [blame^] | 34 | int err; |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 35 | struct kref kref; |
| 36 | struct diag_bridge_ops *ops; |
| 37 | struct platform_device *pdev; |
| 38 | }; |
| 39 | struct diag_bridge *__dev; |
| 40 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 41 | int diag_bridge_open(struct diag_bridge_ops *ops) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 42 | { |
| 43 | struct diag_bridge *dev = __dev; |
| 44 | |
| 45 | if (!dev) { |
| 46 | err("dev is null"); |
| 47 | return -ENODEV; |
| 48 | } |
| 49 | |
| 50 | dev->ops = ops; |
Jack Pham | 3ae820f | 2011-12-14 16:21:04 -0800 | [diff] [blame^] | 51 | dev->err = 0; |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 55 | EXPORT_SYMBOL(diag_bridge_open); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 56 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 57 | void diag_bridge_close(void) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 58 | { |
| 59 | struct diag_bridge *dev = __dev; |
| 60 | |
| 61 | dev_dbg(&dev->udev->dev, "%s:\n", __func__); |
| 62 | |
| 63 | usb_kill_anchored_urbs(&dev->submitted); |
| 64 | |
| 65 | dev->ops = 0; |
| 66 | } |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 67 | EXPORT_SYMBOL(diag_bridge_close); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 68 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 69 | static void diag_bridge_read_cb(struct urb *urb) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 70 | { |
| 71 | struct diag_bridge *dev = urb->context; |
| 72 | struct diag_bridge_ops *cbs = dev->ops; |
| 73 | |
| 74 | dev_dbg(&dev->udev->dev, "%s: status:%d actual:%d\n", __func__, |
| 75 | urb->status, urb->actual_length); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 76 | |
Jack Pham | 3ae820f | 2011-12-14 16:21:04 -0800 | [diff] [blame^] | 77 | if (urb->status == -EPROTO) { |
| 78 | /* save error so that subsequent read/write returns ESHUTDOWN */ |
| 79 | dev->err = urb->status; |
| 80 | return; |
| 81 | } |
| 82 | |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 83 | cbs->read_complete_cb(cbs->ctxt, |
| 84 | urb->transfer_buffer, |
| 85 | urb->transfer_buffer_length, |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 86 | urb->status < 0 ? urb->status : urb->actual_length); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 89 | int diag_bridge_read(char *data, size_t size) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 90 | { |
| 91 | struct urb *urb = NULL; |
| 92 | unsigned int pipe; |
| 93 | struct diag_bridge *dev = __dev; |
| 94 | int ret; |
| 95 | |
| 96 | dev_dbg(&dev->udev->dev, "%s:\n", __func__); |
| 97 | |
| 98 | if (!size) { |
| 99 | dev_err(&dev->udev->dev, "invalid size:%d\n", size); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
| 103 | if (!dev->ifc) { |
| 104 | dev_err(&dev->udev->dev, "device is disconnected\n"); |
| 105 | return -ENODEV; |
| 106 | } |
| 107 | |
Jack Pham | 3ae820f | 2011-12-14 16:21:04 -0800 | [diff] [blame^] | 108 | /* if there was a previous unrecoverable error, just quit */ |
| 109 | if (dev->err) |
| 110 | return -ESHUTDOWN; |
| 111 | |
Jack Pham | 290b108 | 2011-12-07 18:41:12 -0800 | [diff] [blame] | 112 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 113 | if (!urb) { |
| 114 | dev_err(&dev->udev->dev, "unable to allocate urb\n"); |
| 115 | return -ENOMEM; |
| 116 | } |
| 117 | |
| 118 | pipe = usb_rcvbulkpipe(dev->udev, dev->in_epAddr); |
| 119 | usb_fill_bulk_urb(urb, dev->udev, pipe, data, size, |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 120 | diag_bridge_read_cb, dev); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 121 | usb_anchor_urb(urb, &dev->submitted); |
| 122 | |
Jack Pham | 290b108 | 2011-12-07 18:41:12 -0800 | [diff] [blame] | 123 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 124 | if (ret) { |
| 125 | dev_err(&dev->udev->dev, "submitting urb failed err:%d\n", ret); |
| 126 | usb_unanchor_urb(urb); |
| 127 | usb_free_urb(urb); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | usb_free_urb(urb); |
| 132 | |
| 133 | return 0; |
| 134 | } |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 135 | EXPORT_SYMBOL(diag_bridge_read); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 136 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 137 | static void diag_bridge_write_cb(struct urb *urb) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 138 | { |
| 139 | struct diag_bridge *dev = urb->context; |
| 140 | struct diag_bridge_ops *cbs = dev->ops; |
| 141 | |
| 142 | dev_dbg(&dev->udev->dev, "%s:\n", __func__); |
| 143 | |
Jack Pham | 3ae820f | 2011-12-14 16:21:04 -0800 | [diff] [blame^] | 144 | if (urb->status == -EPROTO) { |
| 145 | /* save error so that subsequent read/write returns ESHUTDOWN */ |
| 146 | dev->err = urb->status; |
| 147 | return; |
| 148 | } |
| 149 | |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 150 | cbs->write_complete_cb(cbs->ctxt, |
| 151 | urb->transfer_buffer, |
| 152 | urb->transfer_buffer_length, |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 153 | urb->status < 0 ? urb->status : urb->actual_length); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 156 | int diag_bridge_write(char *data, size_t size) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 157 | { |
| 158 | struct urb *urb = NULL; |
| 159 | unsigned int pipe; |
| 160 | struct diag_bridge *dev = __dev; |
| 161 | int ret; |
| 162 | |
| 163 | dev_dbg(&dev->udev->dev, "%s:\n", __func__); |
| 164 | |
| 165 | if (!size) { |
| 166 | dev_err(&dev->udev->dev, "invalid size:%d\n", size); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
| 170 | if (!dev->ifc) { |
| 171 | dev_err(&dev->udev->dev, "device is disconnected\n"); |
| 172 | return -ENODEV; |
| 173 | } |
| 174 | |
Jack Pham | 3ae820f | 2011-12-14 16:21:04 -0800 | [diff] [blame^] | 175 | /* if there was a previous unrecoverable error, just quit */ |
| 176 | if (dev->err) |
| 177 | return -ESHUTDOWN; |
| 178 | |
Jack Pham | 290b108 | 2011-12-07 18:41:12 -0800 | [diff] [blame] | 179 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 180 | if (!urb) { |
| 181 | err("unable to allocate urb"); |
| 182 | return -ENOMEM; |
| 183 | } |
| 184 | |
| 185 | pipe = usb_sndbulkpipe(dev->udev, dev->out_epAddr); |
| 186 | usb_fill_bulk_urb(urb, dev->udev, pipe, data, size, |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 187 | diag_bridge_write_cb, dev); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 188 | usb_anchor_urb(urb, &dev->submitted); |
| 189 | |
Jack Pham | 290b108 | 2011-12-07 18:41:12 -0800 | [diff] [blame] | 190 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 191 | if (ret) { |
| 192 | err("submitting urb failed err:%d", ret); |
| 193 | usb_unanchor_urb(urb); |
| 194 | usb_free_urb(urb); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | usb_free_urb(urb); |
| 199 | |
| 200 | return 0; |
| 201 | } |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 202 | EXPORT_SYMBOL(diag_bridge_write); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 203 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 204 | static void diag_bridge_delete(struct kref *kref) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 205 | { |
| 206 | struct diag_bridge *dev = |
| 207 | container_of(kref, struct diag_bridge, kref); |
| 208 | |
| 209 | usb_put_dev(dev->udev); |
| 210 | __dev = 0; |
| 211 | kfree(dev); |
| 212 | } |
| 213 | |
| 214 | static int |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 215 | diag_bridge_probe(struct usb_interface *ifc, const struct usb_device_id *id) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 216 | { |
| 217 | struct diag_bridge *dev; |
| 218 | struct usb_host_interface *ifc_desc; |
| 219 | struct usb_endpoint_descriptor *ep_desc; |
| 220 | int i; |
| 221 | int ret = -ENOMEM; |
| 222 | __u8 ifc_num; |
| 223 | |
| 224 | dbg("%s: id:%lu", __func__, id->driver_info); |
| 225 | |
| 226 | ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber; |
| 227 | |
| 228 | /* is this interface supported ? */ |
| 229 | if (ifc_num != id->driver_info) |
| 230 | return -ENODEV; |
| 231 | |
| 232 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 233 | if (!dev) { |
| 234 | pr_err("%s: unable to allocate dev\n", __func__); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | dev->pdev = platform_device_alloc("diag_bridge", -1); |
| 238 | if (!dev->pdev) { |
| 239 | pr_err("%s: unable to allocate platform device\n", __func__); |
| 240 | kfree(dev); |
| 241 | return -ENOMEM; |
| 242 | } |
| 243 | __dev = dev; |
| 244 | |
| 245 | dev->udev = usb_get_dev(interface_to_usbdev(ifc)); |
| 246 | dev->ifc = ifc; |
| 247 | kref_init(&dev->kref); |
| 248 | init_usb_anchor(&dev->submitted); |
| 249 | |
| 250 | ifc_desc = ifc->cur_altsetting; |
| 251 | for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) { |
| 252 | ep_desc = &ifc_desc->endpoint[i].desc; |
| 253 | |
| 254 | if (!dev->in_epAddr && usb_endpoint_is_bulk_in(ep_desc)) |
| 255 | dev->in_epAddr = ep_desc->bEndpointAddress; |
| 256 | |
| 257 | if (!dev->out_epAddr && usb_endpoint_is_bulk_out(ep_desc)) |
| 258 | dev->out_epAddr = ep_desc->bEndpointAddress; |
| 259 | } |
| 260 | |
| 261 | if (!(dev->in_epAddr && dev->out_epAddr)) { |
| 262 | err("could not find bulk in and bulk out endpoints"); |
| 263 | ret = -ENODEV; |
| 264 | goto error; |
| 265 | } |
| 266 | |
| 267 | usb_set_intfdata(ifc, dev); |
| 268 | |
| 269 | platform_device_add(dev->pdev); |
| 270 | |
| 271 | dev_dbg(&dev->udev->dev, "%s: complete\n", __func__); |
| 272 | |
| 273 | return 0; |
| 274 | |
| 275 | error: |
| 276 | if (dev) |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 277 | kref_put(&dev->kref, diag_bridge_delete); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 282 | static void diag_bridge_disconnect(struct usb_interface *ifc) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 283 | { |
| 284 | struct diag_bridge *dev = usb_get_intfdata(ifc); |
| 285 | |
| 286 | dev_dbg(&dev->udev->dev, "%s:\n", __func__); |
| 287 | |
| 288 | platform_device_del(dev->pdev); |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 289 | kref_put(&dev->kref, diag_bridge_delete); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 290 | usb_set_intfdata(ifc, NULL); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | #define VALID_INTERFACE_NUM 0 |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 295 | static const struct usb_device_id diag_bridge_ids[] = { |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 296 | { USB_DEVICE(0x5c6, 0x9001), |
| 297 | .driver_info = VALID_INTERFACE_NUM, }, |
| 298 | |
| 299 | {} /* terminating entry */ |
| 300 | }; |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 301 | MODULE_DEVICE_TABLE(usb, diag_bridge_ids); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 302 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 303 | static struct usb_driver diag_bridge_driver = { |
| 304 | .name = "diag_bridge", |
| 305 | .probe = diag_bridge_probe, |
| 306 | .disconnect = diag_bridge_disconnect, |
| 307 | .id_table = diag_bridge_ids, |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 310 | static int __init diag_bridge_init(void) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 311 | { |
| 312 | int ret; |
| 313 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 314 | ret = usb_register(&diag_bridge_driver); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 315 | if (ret) { |
| 316 | err("%s: unable to register diag driver", |
| 317 | __func__); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 324 | static void __exit diag_bridge_exit(void) |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 325 | { |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 326 | usb_deregister(&diag_bridge_driver); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 329 | module_init(diag_bridge_init); |
| 330 | module_exit(diag_bridge_exit); |
Jack Pham | e32cf32 | 2011-09-26 10:20:17 -0700 | [diff] [blame] | 331 | |
| 332 | MODULE_DESCRIPTION(DRIVER_DESC); |
Jack Pham | f6ed558 | 2011-11-02 16:08:31 -0700 | [diff] [blame] | 333 | MODULE_VERSION(DRIVER_VERSION); |
| 334 | MODULE_LICENSE("GPL v2"); |