cyberjack.c (f9cd49033b349b8be3bb1f01b39eed837853d880) cyberjack.c (96fc8e8d8551f4d67b54982175b412c4e1b8d395)
1/*
2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
3 *
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
6 *
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
8 *

--- 122 unchanged lines hidden (view full) ---

131
132 for (i = 0; i < serial->num_ports; ++i) {
133 int result;
134 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
135 GFP_KERNEL);
136 if (result)
137 dev_err(&serial->dev->dev,
138 "usb_submit_urb(read int) failed\n");
1/*
2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
3 *
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
6 *
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
8 *

--- 122 unchanged lines hidden (view full) ---

131
132 for (i = 0; i < serial->num_ports; ++i) {
133 int result;
134 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
135 GFP_KERNEL);
136 if (result)
137 dev_err(&serial->dev->dev,
138 "usb_submit_urb(read int) failed\n");
139 dbg("%s - usb_submit_urb(int urb)", __func__);
139 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n",
140 __func__);
140 }
141
142 return 0;
143}
144
145static void cyberjack_disconnect(struct usb_serial *serial)
146{
147 int i;

--- 14 unchanged lines hidden (view full) ---

162
163static int cyberjack_open(struct tty_struct *tty,
164 struct usb_serial_port *port)
165{
166 struct cyberjack_private *priv;
167 unsigned long flags;
168 int result = 0;
169
141 }
142
143 return 0;
144}
145
146static void cyberjack_disconnect(struct usb_serial *serial)
147{
148 int i;

--- 14 unchanged lines hidden (view full) ---

163
164static int cyberjack_open(struct tty_struct *tty,
165 struct usb_serial_port *port)
166{
167 struct cyberjack_private *priv;
168 unsigned long flags;
169 int result = 0;
170
170 dbg("%s - usb_clear_halt", __func__);
171 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
171 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
172
173 priv = usb_get_serial_port_data(port);
174 spin_lock_irqsave(&priv->lock, flags);
175 priv->rdtodo = 0;
176 priv->wrfilled = 0;
177 priv->wrsent = 0;
178 spin_unlock_irqrestore(&priv->lock, flags);

--- 8 unchanged lines hidden (view full) ---

187 usb_kill_urb(port->write_urb);
188 usb_kill_urb(port->read_urb);
189 }
190}
191
192static int cyberjack_write(struct tty_struct *tty,
193 struct usb_serial_port *port, const unsigned char *buf, int count)
194{
172 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
173
174 priv = usb_get_serial_port_data(port);
175 spin_lock_irqsave(&priv->lock, flags);
176 priv->rdtodo = 0;
177 priv->wrfilled = 0;
178 priv->wrsent = 0;
179 spin_unlock_irqrestore(&priv->lock, flags);

--- 8 unchanged lines hidden (view full) ---

188 usb_kill_urb(port->write_urb);
189 usb_kill_urb(port->read_urb);
190 }
191}
192
193static int cyberjack_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count)
195{
196 struct device *dev = &port->dev;
195 struct cyberjack_private *priv = usb_get_serial_port_data(port);
196 unsigned long flags;
197 int result;
198 int wrexpected;
199
200 if (count == 0) {
197 struct cyberjack_private *priv = usb_get_serial_port_data(port);
198 unsigned long flags;
199 int result;
200 int wrexpected;
201
202 if (count == 0) {
201 dbg("%s - write request of 0 bytes", __func__);
203 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
202 return 0;
203 }
204
205 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
204 return 0;
205 }
206
207 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
206 dbg("%s - already writing", __func__);
208 dev_dbg(dev, "%s - already writing\n", __func__);
207 return 0;
208 }
209
210 spin_lock_irqsave(&priv->lock, flags);
211
212 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
213 /* To much data for buffer. Reset buffer. */
214 priv->wrfilled = 0;
215 spin_unlock_irqrestore(&priv->lock, flags);
216 set_bit(0, &port->write_urbs_free);
217 return 0;
218 }
219
220 /* Copy data */
221 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
222
209 return 0;
210 }
211
212 spin_lock_irqsave(&priv->lock, flags);
213
214 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
215 /* To much data for buffer. Reset buffer. */
216 priv->wrfilled = 0;
217 spin_unlock_irqrestore(&priv->lock, flags);
218 set_bit(0, &port->write_urbs_free);
219 return 0;
220 }
221
222 /* Copy data */
223 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
224
223 usb_serial_debug_data(debug, &port->dev, __func__, count,
225 usb_serial_debug_data(debug, dev, __func__, count,
224 priv->wrbuf + priv->wrfilled);
225 priv->wrfilled += count;
226
227 if (priv->wrfilled >= 3) {
228 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
226 priv->wrbuf + priv->wrfilled);
227 priv->wrfilled += count;
228
229 if (priv->wrfilled >= 3) {
230 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
229 dbg("%s - expected data: %d", __func__, wrexpected);
231 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
230 } else
231 wrexpected = sizeof(priv->wrbuf);
232
233 if (priv->wrfilled >= wrexpected) {
234 /* We have enough data to begin transmission */
235 int length;
236
232 } else
233 wrexpected = sizeof(priv->wrbuf);
234
235 if (priv->wrfilled >= wrexpected) {
236 /* We have enough data to begin transmission */
237 int length;
238
237 dbg("%s - transmitting data (frame 1)", __func__);
239 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
238 length = (wrexpected > port->bulk_out_size) ?
239 port->bulk_out_size : wrexpected;
240
241 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
242 priv->wrsent = length;
243
244 /* set up our urb */
245 port->write_urb->transfer_buffer_length = length;

--- 7 unchanged lines hidden (view full) ---

253 /* Throw away data. No better idea what to do with it. */
254 priv->wrfilled = 0;
255 priv->wrsent = 0;
256 spin_unlock_irqrestore(&priv->lock, flags);
257 set_bit(0, &port->write_urbs_free);
258 return 0;
259 }
260
240 length = (wrexpected > port->bulk_out_size) ?
241 port->bulk_out_size : wrexpected;
242
243 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
244 priv->wrsent = length;
245
246 /* set up our urb */
247 port->write_urb->transfer_buffer_length = length;

--- 7 unchanged lines hidden (view full) ---

255 /* Throw away data. No better idea what to do with it. */
256 priv->wrfilled = 0;
257 priv->wrsent = 0;
258 spin_unlock_irqrestore(&priv->lock, flags);
259 set_bit(0, &port->write_urbs_free);
260 return 0;
261 }
262
261 dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
262 dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
263 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
264 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
263
264 if (priv->wrsent >= priv->wrfilled) {
265
266 if (priv->wrsent >= priv->wrfilled) {
265 dbg("%s - buffer cleaned", __func__);
267 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
266 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
267 priv->wrfilled = 0;
268 priv->wrsent = 0;
269 }
270 }
271
272 spin_unlock_irqrestore(&priv->lock, flags);
273

--- 5 unchanged lines hidden (view full) ---

279 /* FIXME: .... */
280 return CYBERJACK_LOCAL_BUF_SIZE;
281}
282
283static void cyberjack_read_int_callback(struct urb *urb)
284{
285 struct usb_serial_port *port = urb->context;
286 struct cyberjack_private *priv = usb_get_serial_port_data(port);
268 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
269 priv->wrfilled = 0;
270 priv->wrsent = 0;
271 }
272 }
273
274 spin_unlock_irqrestore(&priv->lock, flags);
275

--- 5 unchanged lines hidden (view full) ---

281 /* FIXME: .... */
282 return CYBERJACK_LOCAL_BUF_SIZE;
283}
284
285static void cyberjack_read_int_callback(struct urb *urb)
286{
287 struct usb_serial_port *port = urb->context;
288 struct cyberjack_private *priv = usb_get_serial_port_data(port);
289 struct device *dev = &port->dev;
287 unsigned char *data = urb->transfer_buffer;
288 int status = urb->status;
289 int result;
290
291 /* the urb might have been killed. */
292 if (status)
293 return;
294
290 unsigned char *data = urb->transfer_buffer;
291 int status = urb->status;
292 int result;
293
294 /* the urb might have been killed. */
295 if (status)
296 return;
297
295 usb_serial_debug_data(debug, &port->dev, __func__,
296 urb->actual_length, data);
298 usb_serial_debug_data(debug, dev, __func__, urb->actual_length, data);
297
298 /* React only to interrupts signaling a bulk_in transfer */
299 if (urb->actual_length == 4 && data[0] == 0x01) {
300 short old_rdtodo;
301
302 /* This is a announcement of coming bulk_ins. */
303 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
304
305 spin_lock(&priv->lock);
306
307 old_rdtodo = priv->rdtodo;
308
309 if (old_rdtodo + size < old_rdtodo) {
299
300 /* React only to interrupts signaling a bulk_in transfer */
301 if (urb->actual_length == 4 && data[0] == 0x01) {
302 short old_rdtodo;
303
304 /* This is a announcement of coming bulk_ins. */
305 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
306
307 spin_lock(&priv->lock);
308
309 old_rdtodo = priv->rdtodo;
310
311 if (old_rdtodo + size < old_rdtodo) {
310 dbg("To many bulk_in urbs to do.");
312 dev_dbg(dev, "To many bulk_in urbs to do.\n");
311 spin_unlock(&priv->lock);
312 goto resubmit;
313 }
314
315 /* "+=" is probably more fault tollerant than "=" */
316 priv->rdtodo += size;
317
313 spin_unlock(&priv->lock);
314 goto resubmit;
315 }
316
317 /* "+=" is probably more fault tollerant than "=" */
318 priv->rdtodo += size;
319
318 dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
320 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
319
320 spin_unlock(&priv->lock);
321
322 if (!old_rdtodo) {
323 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
324 if (result)
321
322 spin_unlock(&priv->lock);
323
324 if (!old_rdtodo) {
325 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
326 if (result)
325 dev_err(&port->dev, "%s - failed resubmitting "
326 "read urb, error %d\n",
327 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
327 __func__, result);
328 __func__, result);
328 dbg("%s - usb_submit_urb(read urb)", __func__);
329 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
329 }
330 }
331
332resubmit:
333 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
334 if (result)
335 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
330 }
331 }
332
333resubmit:
334 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
335 if (result)
336 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
336 dbg("%s - usb_submit_urb(int urb)", __func__);
337 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
337}
338
339static void cyberjack_read_bulk_callback(struct urb *urb)
340{
341 struct usb_serial_port *port = urb->context;
342 struct cyberjack_private *priv = usb_get_serial_port_data(port);
338}
339
340static void cyberjack_read_bulk_callback(struct urb *urb)
341{
342 struct usb_serial_port *port = urb->context;
343 struct cyberjack_private *priv = usb_get_serial_port_data(port);
344 struct device *dev = &port->dev;
343 struct tty_struct *tty;
344 unsigned char *data = urb->transfer_buffer;
345 short todo;
346 int result;
347 int status = urb->status;
348
349 usb_serial_debug_data(debug, &port->dev, __func__,
350 urb->actual_length, data);
351 if (status) {
345 struct tty_struct *tty;
346 unsigned char *data = urb->transfer_buffer;
347 short todo;
348 int result;
349 int status = urb->status;
350
351 usb_serial_debug_data(debug, &port->dev, __func__,
352 urb->actual_length, data);
353 if (status) {
352 dbg("%s - nonzero read bulk status received: %d",
353 __func__, status);
354 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
355 __func__, status);
354 return;
355 }
356
357 tty = tty_port_tty_get(&port->port);
358 if (!tty) {
356 return;
357 }
358
359 tty = tty_port_tty_get(&port->port);
360 if (!tty) {
359 dbg("%s - ignoring since device not open", __func__);
361 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
360 return;
361 }
362 if (urb->actual_length) {
363 tty_insert_flip_string(tty, data, urb->actual_length);
364 tty_flip_buffer_push(tty);
365 }
366 tty_kref_put(tty);
367
368 spin_lock(&priv->lock);
369
370 /* Reduce urbs to do by one. */
371 priv->rdtodo -= urb->actual_length;
372 /* Just to be sure */
373 if (priv->rdtodo < 0)
374 priv->rdtodo = 0;
375 todo = priv->rdtodo;
376
377 spin_unlock(&priv->lock);
378
362 return;
363 }
364 if (urb->actual_length) {
365 tty_insert_flip_string(tty, data, urb->actual_length);
366 tty_flip_buffer_push(tty);
367 }
368 tty_kref_put(tty);
369
370 spin_lock(&priv->lock);
371
372 /* Reduce urbs to do by one. */
373 priv->rdtodo -= urb->actual_length;
374 /* Just to be sure */
375 if (priv->rdtodo < 0)
376 priv->rdtodo = 0;
377 todo = priv->rdtodo;
378
379 spin_unlock(&priv->lock);
380
379 dbg("%s - rdtodo: %d", __func__, todo);
381 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
380
381 /* Continue to read if we have still urbs to do. */
382 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
383 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
384 if (result)
382
383 /* Continue to read if we have still urbs to do. */
384 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
385 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
386 if (result)
385 dev_err(&port->dev, "%s - failed resubmitting read "
386 "urb, error %d\n", __func__, result);
387 dbg("%s - usb_submit_urb(read urb)", __func__);
387 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
388 __func__, result);
389 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
388 }
389}
390
391static void cyberjack_write_bulk_callback(struct urb *urb)
392{
393 struct usb_serial_port *port = urb->context;
394 struct cyberjack_private *priv = usb_get_serial_port_data(port);
390 }
391}
392
393static void cyberjack_write_bulk_callback(struct urb *urb)
394{
395 struct usb_serial_port *port = urb->context;
396 struct cyberjack_private *priv = usb_get_serial_port_data(port);
397 struct device *dev = &port->dev;
395 int status = urb->status;
396
397 set_bit(0, &port->write_urbs_free);
398 if (status) {
398 int status = urb->status;
399
400 set_bit(0, &port->write_urbs_free);
401 if (status) {
399 dbg("%s - nonzero write bulk status received: %d",
400 __func__, status);
402 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
403 __func__, status);
401 return;
402 }
403
404 spin_lock(&priv->lock);
405
406 /* only do something if we have more data to send */
407 if (priv->wrfilled) {
408 int length, blksize, result;
409
404 return;
405 }
406
407 spin_lock(&priv->lock);
408
409 /* only do something if we have more data to send */
410 if (priv->wrfilled) {
411 int length, blksize, result;
412
410 dbg("%s - transmitting data (frame n)", __func__);
413 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
411
412 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
413 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
414
415 memcpy(port->write_urb->transfer_buffer,
416 priv->wrbuf + priv->wrsent, length);
417 priv->wrsent += length;
418
419 /* set up our urb */
420 port->write_urb->transfer_buffer_length = length;
421
422 /* send the data out the bulk port */
423 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
424 if (result) {
414
415 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
416 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
417
418 memcpy(port->write_urb->transfer_buffer,
419 priv->wrbuf + priv->wrsent, length);
420 priv->wrsent += length;
421
422 /* set up our urb */
423 port->write_urb->transfer_buffer_length = length;
424
425 /* send the data out the bulk port */
426 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
427 if (result) {
425 dev_err(&port->dev,
426 "%s - failed submitting write urb, error %d\n",
428 dev_err(dev, "%s - failed submitting write urb, error %d\n",
427 __func__, result);
428 /* Throw away data. No better idea what to do with it. */
429 priv->wrfilled = 0;
430 priv->wrsent = 0;
431 goto exit;
432 }
433
429 __func__, result);
430 /* Throw away data. No better idea what to do with it. */
431 priv->wrfilled = 0;
432 priv->wrsent = 0;
433 goto exit;
434 }
435
434 dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
435 dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
436 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
437 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
436
437 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
438
439 if (priv->wrsent >= priv->wrfilled ||
440 priv->wrsent >= blksize) {
438
439 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
440
441 if (priv->wrsent >= priv->wrfilled ||
442 priv->wrsent >= blksize) {
441 dbg("%s - buffer cleaned", __func__);
443 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
442 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
443 priv->wrfilled = 0;
444 priv->wrsent = 0;
445 }
446 }
447
448exit:
449 spin_unlock(&priv->lock);

--- 12 unchanged lines hidden ---
444 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
445 priv->wrfilled = 0;
446 priv->wrsent = 0;
447 }
448 }
449
450exit:
451 spin_unlock(&priv->lock);

--- 12 unchanged lines hidden ---