diff options
Diffstat (limited to 'drivers/misc/ti-st')
| -rw-r--r-- | drivers/misc/ti-st/gps_drv.c | 804 |
1 files changed, 804 insertions, 0 deletions
diff --git a/drivers/misc/ti-st/gps_drv.c b/drivers/misc/ti-st/gps_drv.c new file mode 100644 index 00000000000..17ca92dd177 --- /dev/null +++ b/drivers/misc/ti-st/gps_drv.c | |||
| @@ -0,0 +1,804 @@ | |||
| 1 | /* | ||
| 2 | * GPS Char Driver for Texas Instrument's Connectivity Chip. | ||
| 3 | * Copyright (C) 2009 Texas Instruments | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License version 2 as | ||
| 7 | * published by the Free Software Foundation. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <linux/cdev.h> | ||
| 20 | #include <linux/fs.h> | ||
| 21 | #include <linux/device.h> | ||
| 22 | |||
| 23 | #include <linux/uaccess.h> | ||
| 24 | #include <linux/tty.h> | ||
| 25 | #include <linux/sched.h> | ||
| 26 | |||
| 27 | #include <linux/delay.h> | ||
| 28 | #include <linux/firmware.h> | ||
| 29 | #include <linux/platform_device.h> | ||
| 30 | #include <linux/poll.h> | ||
| 31 | #include <linux/skbuff.h> | ||
| 32 | #include <linux/interrupt.h> | ||
| 33 | |||
| 34 | #include <linux/ti_wilink_st.h> | ||
| 35 | |||
| 36 | #undef VERBOSE | ||
| 37 | #undef DEBUG | ||
| 38 | |||
| 39 | /* Debug macros*/ | ||
| 40 | #if defined(DEBUG) /* limited debug messages */ | ||
| 41 | #define GPSDRV_DBG(fmt, arg...) \ | ||
| 42 | printk(KERN_INFO "[GPS] (gpsdrv):"fmt"\n" , ## arg) | ||
| 43 | #define GPSDRV_VER(fmt, arg...) | ||
| 44 | #elif defined(VERBOSE) /* very verbose */ | ||
| 45 | #define GPSDRV_DBG(fmt, arg...) \ | ||
| 46 | printk(KERN_INFO "[GPS] (gpsdrv):"fmt"\n" , ## arg) | ||
| 47 | #define GPSDRV_VER(fmt, arg...) \ | ||
| 48 | printk(KERN_INFO "[GPS] (gpsdrv):"fmt"\n" , ## arg) | ||
| 49 | #define GPSDRV_ERR(fmt, arg...) \ | ||
| 50 | printk(KERN_ERR "[GPS] (gpsdrv):"fmt"\n" , ## arg) | ||
| 51 | #else /* Error msgs only */ | ||
| 52 | #define GPSDRV_ERR(fmt, arg...) \ | ||
| 53 | printk(KERN_ERR "[GPS] (gpsdrv):"fmt"\n" , ## arg) | ||
| 54 | #define GPSDRV_VER(fmt, arg...) | ||
| 55 | #define GPSDRV_DBG(fmt, arg...) | ||
| 56 | #endif | ||
| 57 | |||
| 58 | static void gpsdrv_tsklet_write(unsigned long data); | ||
| 59 | |||
| 60 | /* List of error codes returned by the gps driver*/ | ||
| 61 | enum { | ||
| 62 | GPS_ERR_FAILURE = -1, /* check struct */ | ||
| 63 | GPS_SUCCESS, | ||
| 64 | GPS_ERR_CLASS = -15, | ||
| 65 | GPS_ERR_CPY_TO_USR, | ||
| 66 | GPS_ERR_CPY_FRM_USR, | ||
| 67 | GPS_ERR_UNKNOWN, | ||
| 68 | }; | ||
| 69 | |||
| 70 | /* Channel-9 details for GPS */ | ||
| 71 | #define GPS_CH9_PKT_HDR_SIZE 4 | ||
| 72 | #define GPS_CH9_PKT_NUMBER 0x9 | ||
| 73 | #define GPS_CH9_OP_WRITE 0x1 | ||
| 74 | #define GPS_CH9_OP_READ 0x2 | ||
| 75 | #define GPS_CH9_OP_COMPLETED_EVT 0x3 | ||
| 76 | |||
| 77 | /* Macros for Syncronising GPS registration and other R/W/ICTL operations */ | ||
| 78 | #define GPS_ST_REGISTERED 0 | ||
| 79 | #define GPS_ST_RUNNING 1 | ||
| 80 | |||
| 81 | /* Read time out defined to 10 seconds */ | ||
| 82 | #define GPSDRV_READ_TIMEOUT 10000 | ||
| 83 | /* Reg time out defined to 6 seconds */ | ||
| 84 | #define GPSDRV_REG_TIMEOUT 6000 | ||
| 85 | |||
| 86 | |||
| 87 | struct gpsdrv_event_hdr { | ||
| 88 | uint8_t opcode; | ||
| 89 | uint16_t plen; | ||
| 90 | } __packed; | ||
| 91 | |||
| 92 | /* | ||
| 93 | * struct gpsdrv_data - gps internal driver data | ||
| 94 | * @gpsdrv_reg_completed - completion to wait for registration | ||
| 95 | * @streg_cbdata - registration feedback | ||
| 96 | * @state - driver state | ||
| 97 | * @tx_count - TX throttling/unthrottling | ||
| 98 | * @st_write - write ptr from ST | ||
| 99 | * @rx_list - Rx data SKB queue | ||
| 100 | * @tx_list - Tx data SKB queue | ||
| 101 | * @gpsdrv_data_q - dataq checked up on poll/receive | ||
| 102 | * @lock - spin lock | ||
| 103 | * @gpsdrv_tx_tsklet - gps write task | ||
| 104 | */ | ||
| 105 | |||
| 106 | struct gpsdrv_data { | ||
| 107 | struct completion gpsdrv_reg_completed; | ||
| 108 | char streg_cbdata; | ||
| 109 | unsigned long state; | ||
| 110 | unsigned char tx_count; | ||
| 111 | long (*st_write) (struct sk_buff *skb); | ||
| 112 | struct sk_buff_head rx_list; | ||
| 113 | struct sk_buff_head tx_list; | ||
| 114 | wait_queue_head_t gpsdrv_data_q; | ||
| 115 | spinlock_t lock; | ||
| 116 | struct tasklet_struct gpsdrv_tx_tsklet; | ||
| 117 | }; | ||
| 118 | |||
| 119 | #define DEVICE_NAME "tigps" | ||
| 120 | |||
| 121 | /***********Functions called from ST driver**********************************/ | ||
| 122 | |||
| 123 | /* gpsdrv_st_recv Function | ||
| 124 | * This is Called in from -- ST Core when a data is received | ||
| 125 | * This is a registered callback with ST core when the gps driver registers | ||
| 126 | * with ST. | ||
| 127 | * | ||
| 128 | * Parameters: | ||
| 129 | * @skb : SKB buffer pointer which contains the incoming Ch-9 GPS data. | ||
| 130 | * Returns: | ||
| 131 | * GPS_SUCCESS - On Success | ||
| 132 | * else suitable error code | ||
| 133 | */ | ||
| 134 | long gpsdrv_st_recv(void *arg, struct sk_buff *skb) | ||
| 135 | { | ||
| 136 | struct gpsdrv_event_hdr gpsdrv_hdr = { 0x00, 0x0000 }; | ||
| 137 | struct gpsdrv_data *hgps = (struct gpsdrv_data *)arg; | ||
| 138 | |||
| 139 | /* SKB is NULL */ | ||
| 140 | if (NULL == skb) { | ||
| 141 | GPSDRV_ERR("Input SKB is NULL"); | ||
| 142 | return GPS_ERR_FAILURE; | ||
| 143 | } | ||
| 144 | |||
| 145 | /* Sanity Check - To Check if the Rx Pkt is Channel -9 or not */ | ||
| 146 | if (0x09 != skb->cb[0]) { | ||
| 147 | GPSDRV_ERR("Input SKB is not a Channel-9 packet"); | ||
| 148 | return GPS_ERR_FAILURE; | ||
| 149 | } | ||
| 150 | /* Copy Ch-9 info to local structure */ | ||
| 151 | memcpy(&gpsdrv_hdr, skb->data, GPS_CH9_PKT_HDR_SIZE - 1); | ||
| 152 | skb_pull(skb, GPS_CH9_PKT_HDR_SIZE - 1); | ||
| 153 | |||
| 154 | /* check if skb->len and gpsdrv_hdr.plen are equal */ | ||
| 155 | if (skb->len != gpsdrv_hdr.plen) { | ||
| 156 | GPSDRV_ERR("Received corrupted packet - Length Mismatch"); | ||
| 157 | return -EINVAL; | ||
| 158 | } | ||
| 159 | #ifdef VERBOSE | ||
| 160 | printk(KERN_INFO"data start >>\n"); | ||
| 161 | print_hex_dump(KERN_INFO, ">in>", DUMP_PREFIX_NONE, | ||
| 162 | 16, 1, skb->data, skb->len, 0); | ||
| 163 | printk(KERN_INFO"\n<< end\n"); | ||
| 164 | #endif | ||
| 165 | /* Check the Opcode */ | ||
| 166 | if ((gpsdrv_hdr.opcode != GPS_CH9_OP_READ) && (gpsdrv_hdr.opcode != \ | ||
| 167 | GPS_CH9_OP_COMPLETED_EVT)) { | ||
| 168 | GPSDRV_ERR("Rec corrupt pkt opcode %x", gpsdrv_hdr.opcode); | ||
| 169 | return -EINVAL; | ||
| 170 | } | ||
| 171 | |||
| 172 | /* Strip Channel 9 packet information from SKB only | ||
| 173 | * if the opcode is GPS_CH9_OP_READ and get AI2 packet | ||
| 174 | */ | ||
| 175 | if (GPS_CH9_OP_READ == gpsdrv_hdr.opcode) { | ||
| 176 | skb_queue_tail(&hgps->rx_list, skb); | ||
| 177 | wake_up_interruptible(&hgps->gpsdrv_data_q); | ||
| 178 | } else { | ||
| 179 | spin_lock(&hgps->lock); | ||
| 180 | /* The no. of Completed Packets is always 1 | ||
| 181 | * in case of Channel 9 as per spec. | ||
| 182 | * Forcing it to 1 for precaution. | ||
| 183 | */ | ||
| 184 | hgps->tx_count = 1; | ||
| 185 | /* Check if Tx queue and Tx count not empty */ | ||
| 186 | if (!skb_queue_empty(&hgps->tx_list)) { | ||
| 187 | /* Schedule the Tx-task let */ | ||
| 188 | spin_unlock(&hgps->lock); | ||
| 189 | GPSDRV_VER(" Scheduling tasklet to write"); | ||
| 190 | tasklet_schedule(&hgps->gpsdrv_tx_tsklet); | ||
| 191 | } else { | ||
| 192 | spin_unlock(&hgps->lock); | ||
| 193 | } | ||
| 194 | |||
| 195 | GPSDRV_VER(" Tx count = %x", hgps->tx_count); | ||
| 196 | /* Free the received command complete SKB */ | ||
| 197 | kfree_skb(skb); | ||
| 198 | } | ||
| 199 | |||
| 200 | return GPS_SUCCESS; | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | /* gpsdrv_st_cb Function | ||
| 205 | * This is Called in from -- ST Core when the state is pending during | ||
| 206 | * st_register. This is a registered callback with ST core when the gps | ||
| 207 | * driver registers with ST. | ||
| 208 | * | ||
| 209 | * Parameters: | ||
| 210 | * @data Status update of GPS registration | ||
| 211 | * Returns: NULL | ||
| 212 | */ | ||
| 213 | void gpsdrv_st_cb(void *arg, char data) | ||
| 214 | { | ||
| 215 | struct gpsdrv_data *hgps = (struct gpsdrv_data *)arg; | ||
| 216 | |||
| 217 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 218 | hgps->streg_cbdata = data; /* ST registration callback status */ | ||
| 219 | complete_all(&hgps->gpsdrv_reg_completed); | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | static struct st_proto_s gpsdrv_proto = { | ||
| 224 | .chnl_id = 0x09, | ||
| 225 | .max_frame_size = 1024, | ||
| 226 | .hdr_len = 3, | ||
| 227 | .offset_len_in_hdr = 1, | ||
| 228 | .len_size = 2, | ||
| 229 | .reserve = 1, | ||
| 230 | .recv = gpsdrv_st_recv, | ||
| 231 | .reg_complete_cb = gpsdrv_st_cb, | ||
| 232 | }; | ||
| 233 | |||
| 234 | /** gpsdrv_tsklet_write Function | ||
| 235 | * This tasklet function will be scheduled when there is a data in Tx queue | ||
| 236 | * and GPS chip sent an command completion packet with non zero value. | ||
| 237 | * | ||
| 238 | * Parameters : | ||
| 239 | * @data : data passed to tasklet function | ||
| 240 | * Returns : NULL | ||
| 241 | */ | ||
| 242 | void gpsdrv_tsklet_write(unsigned long data) | ||
| 243 | { | ||
| 244 | struct sk_buff *skb = NULL; | ||
| 245 | struct gpsdrv_data *hgps = (struct gpsdrv_data *)data; | ||
| 246 | |||
| 247 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 248 | |||
| 249 | spin_lock(&hgps->lock); | ||
| 250 | |||
| 251 | /* Perform sanity check of verifying the status | ||
| 252 | to perform an st_write */ | ||
| 253 | if (((!hgps->st_write) || (0 == hgps->tx_count)) | ||
| 254 | || ((skb_queue_empty(&hgps->tx_list)))) { | ||
| 255 | spin_unlock(&hgps->lock); | ||
| 256 | GPSDRV_ERR("Sanity check Failed exiting %s", __func__); | ||
| 257 | return; | ||
| 258 | } | ||
| 259 | /* hgps->tx_list not empty skb already present | ||
| 260 | * dequeue the tx-data and perform a st_write | ||
| 261 | */ | ||
| 262 | hgps->tx_count--; | ||
| 263 | spin_unlock(&hgps->lock); | ||
| 264 | GPSDRV_VER(" Tx count in gpsdrv_tsklet_write = %x", hgps->tx_count); | ||
| 265 | skb = skb_dequeue(&hgps->tx_list); | ||
| 266 | hgps->st_write(skb); | ||
| 267 | |||
| 268 | return; | ||
| 269 | } | ||
| 270 | |||
| 271 | /*********Functions Called from GPS host***************************************/ | ||
| 272 | |||
| 273 | /** gpsdrv_open Function | ||
| 274 | * This function will perform an register on ST driver. | ||
| 275 | * | ||
| 276 | * Parameters : | ||
| 277 | * @file : File pointer for GPS char driver | ||
| 278 | * @inod : | ||
| 279 | * Returns GPS_SUCCESS - on success | ||
| 280 | * else suitable error code | ||
| 281 | */ | ||
| 282 | int gpsdrv_open(struct inode *inod, struct file *file) | ||
| 283 | { | ||
| 284 | int ret = 0; | ||
| 285 | unsigned long timeout = GPSDRV_REG_TIMEOUT; | ||
| 286 | struct gpsdrv_data *hgps; | ||
| 287 | |||
| 288 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 289 | |||
| 290 | /* Allocate local resource memory */ | ||
| 291 | hgps = kzalloc(sizeof(struct gpsdrv_data), GFP_KERNEL); | ||
| 292 | if (!(hgps)) { | ||
| 293 | GPSDRV_ERR("Can't allocate GPS data structure"); | ||
| 294 | return -ENOMEM; | ||
| 295 | } | ||
| 296 | |||
| 297 | /* Initialize wait queue, skb queue head and | ||
| 298 | * registration complete strucuture | ||
| 299 | */ | ||
| 300 | skb_queue_head_init(&hgps->rx_list); | ||
| 301 | skb_queue_head_init(&hgps->tx_list); | ||
| 302 | init_completion(&hgps->gpsdrv_reg_completed); | ||
| 303 | init_waitqueue_head(&hgps->gpsdrv_data_q); | ||
| 304 | spin_lock_init(&hgps->lock); | ||
| 305 | |||
| 306 | /* Check if GPS is already registered with ST */ | ||
| 307 | if (test_and_set_bit(GPS_ST_REGISTERED, &hgps->state)) { | ||
| 308 | GPSDRV_ERR("GPS Registered/Registration in progress with ST" | ||
| 309 | " ,open called again?"); | ||
| 310 | kfree(hgps); | ||
| 311 | return -EAGAIN; | ||
| 312 | } | ||
| 313 | |||
| 314 | /* Initialize gpsdrv_reg_completed so as to wait for completion | ||
| 315 | * on the same | ||
| 316 | * if st_register returns with a PENDING status | ||
| 317 | */ | ||
| 318 | INIT_COMPLETION(hgps->gpsdrv_reg_completed); | ||
| 319 | |||
| 320 | gpsdrv_proto.priv_data = hgps; | ||
| 321 | /* Resgister GPS with ST */ | ||
| 322 | ret = st_register(&gpsdrv_proto); | ||
| 323 | GPSDRV_VER(" st_register returned %d", ret); | ||
| 324 | |||
| 325 | /* If GPS Registration returned with error, then clear GPS_ST_REGISTERED | ||
| 326 | * for future open calls and return the appropriate error code | ||
| 327 | */ | ||
| 328 | if (ret < 0 && ret != -EINPROGRESS) { | ||
| 329 | GPSDRV_ERR(" st_register failed"); | ||
| 330 | clear_bit(GPS_ST_REGISTERED, &hgps->state); | ||
| 331 | if (ret == -EINPROGRESS) | ||
| 332 | return -EAGAIN; | ||
| 333 | return GPS_ERR_FAILURE; | ||
| 334 | } | ||
| 335 | |||
| 336 | /* if returned status is pending, wait for the completion */ | ||
| 337 | if (ret == -EINPROGRESS) { | ||
| 338 | GPSDRV_VER(" GPS Register waiting for completion "); | ||
| 339 | timeout = wait_for_completion_timeout \ | ||
| 340 | (&hgps->gpsdrv_reg_completed, msecs_to_jiffies(timeout)); | ||
| 341 | /* Check for timed out condition */ | ||
| 342 | if (0 == timeout) { | ||
| 343 | GPSDRV_ERR("GPS Device registration timed out"); | ||
| 344 | clear_bit(GPS_ST_REGISTERED, &hgps->state); | ||
| 345 | return -ETIMEDOUT; | ||
| 346 | } else if (0 > hgps->streg_cbdata) { | ||
| 347 | GPSDRV_ERR("GPS Device Registration Failed-ST\n"); | ||
| 348 | GPSDRV_ERR("RegCB called with "); | ||
| 349 | GPSDRV_ERR("Invalid value %d\n", hgps->streg_cbdata); | ||
| 350 | clear_bit(GPS_ST_REGISTERED, &hgps->state); | ||
| 351 | return -EAGAIN; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | GPSDRV_DBG(" gps registration complete "); | ||
| 355 | |||
| 356 | /* Assign the write callback pointer */ | ||
| 357 | hgps->st_write = gpsdrv_proto.write; | ||
| 358 | hgps->tx_count = 1; | ||
| 359 | file->private_data = hgps; /* set drv data */ | ||
| 360 | tasklet_init(&hgps->gpsdrv_tx_tsklet, (void *)gpsdrv_tsklet_write, | ||
| 361 | (unsigned long)hgps); | ||
| 362 | set_bit(GPS_ST_RUNNING, &hgps->state); | ||
| 363 | |||
| 364 | return GPS_SUCCESS; | ||
| 365 | } | ||
| 366 | |||
| 367 | /** gpsdrv_release Function | ||
| 368 | * This function will un-registers from the ST driver. | ||
| 369 | * | ||
| 370 | * Parameters : | ||
| 371 | * @file : File pointer for GPS char driver | ||
| 372 | * @inod : | ||
| 373 | * Returns GPS_SUCCESS - on success | ||
| 374 | * else suitable error code | ||
| 375 | */ | ||
| 376 | int gpsdrv_release(struct inode *inod, struct file *file) | ||
| 377 | { | ||
| 378 | struct gpsdrv_data *hgps = file->private_data; | ||
| 379 | |||
| 380 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 381 | |||
| 382 | /* Disabling task-let 1st & then un-reg to avoid | ||
| 383 | * tasklet getting scheduled | ||
| 384 | */ | ||
| 385 | tasklet_disable(&hgps->gpsdrv_tx_tsklet); | ||
| 386 | tasklet_kill(&hgps->gpsdrv_tx_tsklet); | ||
| 387 | /* Cleat registered bit if already registered */ | ||
| 388 | if (test_and_clear_bit(GPS_ST_REGISTERED, &hgps->state)) { | ||
| 389 | if (st_unregister(&gpsdrv_proto) < 0) { | ||
| 390 | GPSDRV_ERR(" st_unregister failed"); | ||
| 391 | /* Re-Enable the task-let if un-register fails */ | ||
| 392 | tasklet_enable(&hgps->gpsdrv_tx_tsklet); | ||
| 393 | return GPS_ERR_FAILURE; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | /* Reset Tx count value and st_write function pointer */ | ||
| 398 | hgps->tx_count = 0; | ||
| 399 | hgps->st_write = NULL; | ||
| 400 | clear_bit(GPS_ST_RUNNING, &hgps->state); | ||
| 401 | GPSDRV_VER(" st_unregister success"); | ||
| 402 | |||
| 403 | skb_queue_purge(&hgps->rx_list); | ||
| 404 | skb_queue_purge(&hgps->tx_list); | ||
| 405 | kfree(hgps); | ||
| 406 | file->private_data = NULL; | ||
| 407 | |||
| 408 | return GPS_SUCCESS; | ||
| 409 | } | ||
| 410 | |||
| 411 | /** gpsdrv_read Function | ||
| 412 | * This function will wait till the data received from the ST driver | ||
| 413 | * and then strips the GPS-Channel-9 header from the | ||
| 414 | * incoming AI2 packet and then send it to GPS host application. | ||
| 415 | * | ||
| 416 | * Parameters : | ||
| 417 | * @file : File pointer for GPS char driver | ||
| 418 | * @data : Data which needs to be passed to APP | ||
| 419 | * @size : Length of the data passesd | ||
| 420 | * offset : | ||
| 421 | * Returns Size of AI2 packet received - on success | ||
| 422 | * else suitable error code | ||
| 423 | */ | ||
| 424 | ssize_t gpsdrv_read(struct file *file, char __user *data, size_t size, | ||
| 425 | loff_t *offset) | ||
| 426 | { | ||
| 427 | int len = 0, error = 0; | ||
| 428 | struct sk_buff *skb = NULL; | ||
| 429 | unsigned long timeout = GPSDRV_READ_TIMEOUT; | ||
| 430 | struct gpsdrv_data *hgps; | ||
| 431 | |||
| 432 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 433 | |||
| 434 | /* Validate input parameters */ | ||
| 435 | if ((NULL == file) || (((NULL == data) || (0 == size)))) { | ||
| 436 | GPSDRV_ERR("Invalid input params passed to %s", __func__); | ||
| 437 | return -EINVAL; | ||
| 438 | } | ||
| 439 | |||
| 440 | hgps = file->private_data; | ||
| 441 | /* Check if GPS is registered to perform read operation */ | ||
| 442 | if (!test_bit(GPS_ST_RUNNING, &hgps->state)) { | ||
| 443 | GPSDRV_ERR("GPS Device is not running"); | ||
| 444 | return -EINVAL; | ||
| 445 | } | ||
| 446 | |||
| 447 | /* cannot come here if poll-ed before reading | ||
| 448 | * if not poll-ed wait on the same wait_q | ||
| 449 | */ | ||
| 450 | timeout = wait_event_interruptible_timeout(hgps->gpsdrv_data_q, | ||
| 451 | !skb_queue_empty(&hgps->rx_list), msecs_to_jiffies(timeout)); | ||
| 452 | /* Check for timed out condition */ | ||
| 453 | if (0 == timeout) { | ||
| 454 | GPSDRV_ERR("GPS Device Read timed out"); | ||
| 455 | return -ETIMEDOUT; | ||
| 456 | } | ||
| 457 | |||
| 458 | |||
| 459 | /* hgps->rx_list not empty skb already present */ | ||
| 460 | skb = skb_dequeue(&hgps->rx_list); | ||
| 461 | |||
| 462 | if (!skb) { | ||
| 463 | GPSDRV_ERR("Dequed SKB is NULL?"); | ||
| 464 | return GPS_ERR_UNKNOWN; | ||
| 465 | } else if (skb->len > size) { | ||
| 466 | GPSDRV_DBG("SKB length is Greater than requested size "); | ||
| 467 | GPSDRV_DBG("Returning the available length of SKB\n"); | ||
| 468 | |||
| 469 | error = copy_to_user(data, skb->data, size); | ||
| 470 | skb_pull(skb, size); | ||
| 471 | |||
| 472 | if (skb->len != 0) | ||
| 473 | skb_queue_head(&hgps->rx_list, skb); | ||
| 474 | |||
| 475 | /* printk(KERN_DEBUG "gpsdrv: total size read= %d", size);*/ | ||
| 476 | return size; | ||
| 477 | } | ||
| 478 | |||
| 479 | #ifdef VERBOSE | ||
| 480 | print_hex_dump(KERN_INFO, ">in>", DUMP_PREFIX_NONE, | ||
| 481 | 16, 1, skb->data, skb->len, 0); | ||
| 482 | #endif | ||
| 483 | |||
| 484 | /* Forward the data to the user */ | ||
| 485 | if (skb->len <= size) { | ||
| 486 | if (copy_to_user(data, skb->data, skb->len)) { | ||
| 487 | GPSDRV_ERR(" Unable to copy to user space"); | ||
| 488 | /* Queue the skb back to head */ | ||
| 489 | skb_queue_head(&hgps->rx_list, skb); | ||
| 490 | return GPS_ERR_CPY_TO_USR; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | len = skb->len; | ||
| 495 | kfree_skb(skb); | ||
| 496 | /* printk(KERN_DEBUG "gpsdrv: total size read= %d", len); */ | ||
| 497 | return len; | ||
| 498 | } | ||
| 499 | /* gpsdrv_write Function | ||
| 500 | * This function will pre-pend the GPS-Channel-9 header to the | ||
| 501 | * incoming AI2 packet sent from the GPS host application. | ||
| 502 | * | ||
| 503 | * Parameters : | ||
| 504 | * @file : File pointer for GPS char driver | ||
| 505 | * @data : AI2 packet data from GPS application | ||
| 506 | * @size : Size of the AI2 packet data | ||
| 507 | * @offset : | ||
| 508 | * Returns Size of AI2 packet on success | ||
| 509 | * else suitable error code | ||
| 510 | */ | ||
| 511 | ssize_t gpsdrv_write(struct file *file, const char __user *data, | ||
| 512 | size_t size, loff_t *offset) | ||
| 513 | { | ||
| 514 | unsigned char channel = GPS_CH9_PKT_NUMBER; /* GPS Channel number */ | ||
| 515 | /* Initialize gpsdrv_event_hdr with write opcode */ | ||
| 516 | struct gpsdrv_event_hdr gpsdrv_hdr = { GPS_CH9_OP_WRITE, 0x0000 }; | ||
| 517 | struct sk_buff *skb = NULL; | ||
| 518 | struct gpsdrv_data *hgps; | ||
| 519 | |||
| 520 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 521 | /* Validate input parameters */ | ||
| 522 | if ((NULL == file) || (((NULL == data) || (0 == size)))) { | ||
| 523 | GPSDRV_ERR("Invalid input params passed to %s", __func__); | ||
| 524 | return -EINVAL; | ||
| 525 | } | ||
| 526 | |||
| 527 | hgps = file->private_data; | ||
| 528 | |||
| 529 | /* Check if GPS is registered to perform write operation */ | ||
| 530 | if (!test_bit(GPS_ST_RUNNING, &hgps->state)) { | ||
| 531 | GPSDRV_ERR("GPS Device is not running"); | ||
| 532 | return -EINVAL; | ||
| 533 | } | ||
| 534 | |||
| 535 | if (!hgps->st_write) { | ||
| 536 | GPSDRV_ERR(" Can't write to ST, hgps->st_write null ?"); | ||
| 537 | return -EINVAL; | ||
| 538 | } | ||
| 539 | |||
| 540 | |||
| 541 | skb = alloc_skb(size + GPS_CH9_PKT_HDR_SIZE, GFP_ATOMIC); | ||
| 542 | /* Validate Created SKB */ | ||
| 543 | if (NULL == skb) { | ||
| 544 | GPSDRV_ERR("Error aaloacting SKB"); | ||
| 545 | return -ENOMEM; | ||
| 546 | } | ||
| 547 | |||
| 548 | /* Update chnl-9 information with plen=AI2 pckt size which is "size"*/ | ||
| 549 | gpsdrv_hdr.plen = size; | ||
| 550 | |||
| 551 | /* PrePend Channel-9 header to AI2 packet and write to SKB */ | ||
| 552 | memcpy(skb_put(skb, 1), &channel, 1); | ||
| 553 | memcpy(skb_put(skb, GPS_CH9_PKT_HDR_SIZE - 1), &gpsdrv_hdr, | ||
| 554 | GPS_CH9_PKT_HDR_SIZE - 1); | ||
| 555 | |||
| 556 | /* Forward the data from the user space to ST core */ | ||
| 557 | if (copy_from_user(skb_put(skb, size), data, size)) { | ||
| 558 | GPSDRV_ERR(" Unable to copy from user space"); | ||
| 559 | kfree_skb(skb); | ||
| 560 | return GPS_ERR_CPY_FRM_USR; | ||
| 561 | } | ||
| 562 | |||
| 563 | #ifdef VERBOSE | ||
| 564 | GPSDRV_VER("start data.."); | ||
| 565 | print_hex_dump(KERN_INFO, "<out<", DUMP_PREFIX_NONE, | ||
| 566 | 16, 1, skb->data, size, 0); | ||
| 567 | GPSDRV_VER("\n..end data"); | ||
| 568 | #endif | ||
| 569 | |||
| 570 | /* Check if data can be sent to GPS chip | ||
| 571 | * If not, add it to queue and that can be sent later | ||
| 572 | */ | ||
| 573 | spin_lock(&hgps->lock); | ||
| 574 | if (0 != hgps->tx_count) { | ||
| 575 | /* If TX Q is empty send current SKB; | ||
| 576 | * else, queue current SKB at end of tx_list queue and | ||
| 577 | * send first SKB in tx_list queue. | ||
| 578 | */ | ||
| 579 | hgps->tx_count--; | ||
| 580 | spin_unlock(&hgps->lock); | ||
| 581 | |||
| 582 | GPSDRV_VER(" Tx count in gpsdrv_write = %x", hgps->tx_count); | ||
| 583 | |||
| 584 | if (skb_queue_empty(&hgps->tx_list)) { | ||
| 585 | hgps->st_write(skb); | ||
| 586 | } else { | ||
| 587 | skb_queue_tail(&hgps->tx_list, skb); | ||
| 588 | hgps->st_write(skb_dequeue(&hgps->tx_list)); | ||
| 589 | } | ||
| 590 | } else { | ||
| 591 | /* Add it to TX queue */ | ||
| 592 | spin_unlock(&hgps->lock); | ||
| 593 | GPSDRV_VER(" SKB added to Tx queue "); | ||
| 594 | GPSDRV_VER("Tx count = %x ", hgps->tx_count); | ||
| 595 | skb_queue_tail(&hgps->tx_list, skb); | ||
| 596 | /* This is added for precaution in the case that there is a | ||
| 597 | * context switch during the execution of the above lines. | ||
| 598 | * Redundant otherwise. | ||
| 599 | */ | ||
| 600 | if ((0 != hgps->tx_count) && \ | ||
| 601 | (!skb_queue_empty(&hgps->tx_list))) { | ||
| 602 | /* Schedule the Tx-task let */ | ||
| 603 | GPSDRV_VER("Scheduling tasklet to write\n"); | ||
| 604 | tasklet_schedule(&hgps->gpsdrv_tx_tsklet); | ||
| 605 | } | ||
| 606 | } | ||
| 607 | |||
| 608 | return size; | ||
| 609 | } | ||
| 610 | |||
| 611 | /** gpsdrv_ioctl Function | ||
| 612 | * This will peform the functions as directed by the command and command | ||
| 613 | * argument. | ||
| 614 | * | ||
| 615 | * Parameters : | ||
| 616 | * @file : File pointer for GPS char driver | ||
| 617 | * @cmd : IOCTL Command | ||
| 618 | * @arg : Command argument for IOCTL command | ||
| 619 | * Returns GPS_SUCCESS on success | ||
| 620 | * else suitable error code | ||
| 621 | */ | ||
| 622 | static long gpsdrv_ioctl(struct file *file, | ||
| 623 | unsigned int cmd, unsigned long arg) | ||
| 624 | { | ||
| 625 | struct sk_buff *skb = NULL; | ||
| 626 | int retCode = GPS_SUCCESS; | ||
| 627 | struct gpsdrv_data *hgps; | ||
| 628 | |||
| 629 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 630 | |||
| 631 | /* Validate input parameters */ | ||
| 632 | if ((NULL == file) || (0 == cmd)) { | ||
| 633 | GPSDRV_ERR("Invalid input parameters passed to %s", __func__); | ||
| 634 | return -EINVAL; | ||
| 635 | } | ||
| 636 | |||
| 637 | hgps = file->private_data; | ||
| 638 | |||
| 639 | /* Check if GPS is registered to perform IOCTL operation */ | ||
| 640 | if (!test_bit(GPS_ST_RUNNING, &hgps->state)) { | ||
| 641 | GPSDRV_ERR("GPS Device is not running"); | ||
| 642 | return -EINVAL; | ||
| 643 | } | ||
| 644 | |||
| 645 | switch (cmd) { | ||
| 646 | case TCFLSH: | ||
| 647 | GPSDRV_VER(" IOCTL TCFLSH invoked with %ld argument", arg); | ||
| 648 | switch (arg) { | ||
| 649 | /* purge Rx/Tx SKB list queues depending on arg value */ | ||
| 650 | case TCIFLUSH: | ||
| 651 | skb_queue_purge(&hgps->rx_list); | ||
| 652 | break; | ||
| 653 | case TCOFLUSH: | ||
| 654 | skb_queue_purge(&hgps->tx_list); | ||
| 655 | break; | ||
| 656 | case TCIOFLUSH: | ||
| 657 | skb_queue_purge(&hgps->rx_list); | ||
| 658 | skb_queue_purge(&hgps->tx_list); | ||
| 659 | break; | ||
| 660 | default: | ||
| 661 | GPSDRV_ERR("Invalid Command passed for tcflush"); | ||
| 662 | retCode = 0; | ||
| 663 | break; | ||
| 664 | } | ||
| 665 | break; | ||
| 666 | case FIONREAD: | ||
| 667 | /* Deque the SKB from the head if rx_list is not empty | ||
| 668 | * And update the argument with skb->len to provide | ||
| 669 | * the amount of data available in the available SKB | ||
| 670 | */ | ||
| 671 | skb = skb_dequeue(&hgps->rx_list); | ||
| 672 | if (skb != NULL) { | ||
| 673 | *(unsigned int *)arg = skb->len; | ||
| 674 | /* Re-Store the SKB for furtur Read operations */ | ||
| 675 | skb_queue_head(&hgps->rx_list, skb); | ||
| 676 | } else { | ||
| 677 | *(unsigned int *)arg = 0; | ||
| 678 | } | ||
| 679 | GPSDRV_DBG("returning %d\n", *(unsigned int *)arg); | ||
| 680 | |||
| 681 | break; | ||
| 682 | default: | ||
| 683 | GPSDRV_DBG("Un-Identified IOCTL %d", cmd); | ||
| 684 | retCode = 0; | ||
| 685 | break; | ||
| 686 | } | ||
| 687 | |||
| 688 | return retCode; | ||
| 689 | } | ||
| 690 | |||
| 691 | /** gpsdrv_poll Function | ||
| 692 | * This function will wait till some data is received to the gps driver from ST | ||
| 693 | * | ||
| 694 | * Parameters : | ||
| 695 | * @file : File pointer for GPS char driver | ||
| 696 | * @wait : POLL wait information | ||
| 697 | * Returns status of POLL on success | ||
| 698 | * else suitable error code | ||
| 699 | */ | ||
| 700 | static unsigned int gpsdrv_poll(struct file *file, poll_table *wait) | ||
| 701 | { | ||
| 702 | unsigned long mask = 0; | ||
| 703 | struct gpsdrv_data *hgps = file->private_data; | ||
| 704 | |||
| 705 | /* Check if GPS is registered to perform read operation */ | ||
| 706 | if (!test_bit(GPS_ST_RUNNING, &hgps->state)) { | ||
| 707 | GPSDRV_ERR("GPS Device is not running"); | ||
| 708 | return -EINVAL; | ||
| 709 | } | ||
| 710 | |||
| 711 | /* Wait till data is signalled from gpsdrv_st_recv function | ||
| 712 | * with AI2 packet | ||
| 713 | */ | ||
| 714 | poll_wait(file, &hgps->gpsdrv_data_q, wait); | ||
| 715 | |||
| 716 | if (!skb_queue_empty(&hgps->rx_list)) | ||
| 717 | mask |= POLLIN; /* TODO: check app for mask */ | ||
| 718 | |||
| 719 | return mask; | ||
| 720 | } | ||
| 721 | |||
| 722 | /* GPS Char driver function pointers | ||
| 723 | * These functions are called from USER space by pefroming File Operations | ||
| 724 | * on /dev/gps node exposed by this driver during init | ||
| 725 | */ | ||
| 726 | const struct file_operations gpsdrv_chrdev_ops = { | ||
| 727 | .owner = THIS_MODULE, | ||
| 728 | .open = gpsdrv_open, | ||
| 729 | .read = gpsdrv_read, | ||
| 730 | .write = gpsdrv_write, | ||
| 731 | .unlocked_ioctl = gpsdrv_ioctl, | ||
| 732 | .poll = gpsdrv_poll, | ||
| 733 | .release = gpsdrv_release, | ||
| 734 | }; | ||
| 735 | |||
| 736 | /*********Functions called during insmod and delmod****************************/ | ||
| 737 | |||
| 738 | static int gpsdrv_major; /* GPS major number */ | ||
| 739 | static struct class *gpsdrv_class; /* GPS class during class_create */ | ||
| 740 | static struct device *gpsdrv_dev; /* GPS dev during device_create */ | ||
| 741 | /** gpsdrv_init Function | ||
| 742 | * This function Initializes the gps driver parametes and exposes | ||
| 743 | * /dev/gps node to user space | ||
| 744 | * | ||
| 745 | * Parameters : NULL | ||
| 746 | * Returns GPS_SUCCESS on success | ||
| 747 | * else suitable error code | ||
| 748 | */ | ||
| 749 | static int __init gpsdrv_init(void) | ||
| 750 | { | ||
| 751 | |||
| 752 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 753 | |||
| 754 | /* Expose the device DEVICE_NAME to user space | ||
| 755 | * And obtain the major number for the device | ||
| 756 | */ | ||
| 757 | gpsdrv_major = register_chrdev(0, DEVICE_NAME, \ | ||
| 758 | &gpsdrv_chrdev_ops); | ||
| 759 | if (0 > gpsdrv_major) { | ||
| 760 | GPSDRV_ERR("Error when registering to char dev"); | ||
| 761 | return GPS_ERR_FAILURE; | ||
| 762 | } | ||
| 763 | GPSDRV_VER("allocated %d, %d", gpsdrv_major, 0); | ||
| 764 | |||
| 765 | /* udev */ | ||
| 766 | gpsdrv_class = class_create(THIS_MODULE, DEVICE_NAME); | ||
| 767 | if (IS_ERR(gpsdrv_class)) { | ||
| 768 | GPSDRV_ERR(" Something went wrong in class_create"); | ||
| 769 | unregister_chrdev(gpsdrv_major, DEVICE_NAME); | ||
| 770 | return GPS_ERR_CLASS; | ||
| 771 | } | ||
| 772 | |||
| 773 | gpsdrv_dev = | ||
| 774 | device_create(gpsdrv_class, NULL, MKDEV(gpsdrv_major, 0), | ||
| 775 | NULL, DEVICE_NAME); | ||
| 776 | if (IS_ERR(gpsdrv_dev)) { | ||
| 777 | GPSDRV_ERR(" Error in class_create"); | ||
| 778 | unregister_chrdev(gpsdrv_major, DEVICE_NAME); | ||
| 779 | class_destroy(gpsdrv_class); | ||
| 780 | return GPS_ERR_CLASS; | ||
| 781 | } | ||
| 782 | |||
| 783 | return GPS_SUCCESS; | ||
| 784 | } | ||
| 785 | |||
| 786 | /** gpsdrv_exit Function | ||
| 787 | * This function Destroys the gps driver parametes and /dev/gps node | ||
| 788 | * | ||
| 789 | * Parameters : NULL | ||
| 790 | * Returns NULL | ||
| 791 | */ | ||
| 792 | static void __exit gpsdrv_exit(void) | ||
| 793 | { | ||
| 794 | GPSDRV_DBG(" Inside %s", __func__); | ||
| 795 | GPSDRV_VER(" Bye.. freeing up %d", gpsdrv_major); | ||
| 796 | |||
| 797 | device_destroy(gpsdrv_class, MKDEV(gpsdrv_major, 0)); | ||
| 798 | class_destroy(gpsdrv_class); | ||
| 799 | unregister_chrdev(gpsdrv_major, DEVICE_NAME); | ||
| 800 | } | ||
| 801 | |||
| 802 | module_init(gpsdrv_init); | ||
| 803 | module_exit(gpsdrv_exit); | ||
| 804 | MODULE_LICENSE("GPL"); | ||
