empeg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * USB Empeg empeg-car player driver
  3. *
  4. * Copyright (C) 2000, 2001
  5. * Gary Brubaker (xavyer@ix.netcom.com)
  6. *
  7. * Copyright (C) 1999 - 2001
  8. * Greg Kroah-Hartman (greg@kroah.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, as published by
  12. * the Free Software Foundation, version 2.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this driver
  15. *
  16. * (07/16/2001) gb
  17. * remove unused code in empeg_close() (thanks to Oliver Neukum for pointing this
  18. * out) and rewrote empeg_set_termios().
  19. *
  20. * (05/30/2001) gkh
  21. * switched from using spinlock to a semaphore, which fixes lots of problems.
  22. *
  23. * (04/08/2001) gb
  24. * Identify version on module load.
  25. *
  26. * (01/22/2001) gb
  27. * Added write_room() and chars_in_buffer() support.
  28. *
  29. * (12/21/2000) gb
  30. * Moved termio stuff inside the port->active check.
  31. * Moved MOD_DEC_USE_COUNT to end of empeg_close().
  32. *
  33. * (12/03/2000) gb
  34. * Added port->tty->ldisc.set_termios(port->tty, NULL) to empeg_open()
  35. * This notifies the tty driver that the termios have changed.
  36. *
  37. * (11/13/2000) gb
  38. * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to empeg_open()
  39. * (It only needs to be set once - Doh!)
  40. *
  41. * (11/11/2000) gb
  42. * Updated to work with id_table structure.
  43. *
  44. * (11/04/2000) gb
  45. * Forked this from visor.c, and hacked it up to work with an
  46. * Empeg ltd. empeg-car player. Constructive criticism welcomed.
  47. * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
  48. * use of his code, and for his guidance, advice and patience. :)
  49. * A 'Thank You' is in order for John Ripley of Empeg ltd for his
  50. * advice, and patience too.
  51. *
  52. */
  53. #include <linux/config.h>
  54. #include <linux/kernel.h>
  55. #include <linux/errno.h>
  56. #include <linux/init.h>
  57. #include <linux/slab.h>
  58. #include <linux/tty.h>
  59. #include <linux/tty_driver.h>
  60. #include <linux/tty_flip.h>
  61. #include <linux/module.h>
  62. #include <linux/spinlock.h>
  63. #include <asm/uaccess.h>
  64. #include <linux/usb.h>
  65. #include "usb-serial.h"
  66. static int debug;
  67. /*
  68. * Version Information
  69. */
  70. #define DRIVER_VERSION "v1.2"
  71. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
  72. #define DRIVER_DESC "USB Empeg Mark I/II Driver"
  73. #define EMPEG_VENDOR_ID 0x084f
  74. #define EMPEG_PRODUCT_ID 0x0001
  75. /* function prototypes for an empeg-car player */
  76. static int empeg_open (struct usb_serial_port *port, struct file *filp);
  77. static void empeg_close (struct usb_serial_port *port, struct file *filp);
  78. static int empeg_write (struct usb_serial_port *port,
  79. const unsigned char *buf,
  80. int count);
  81. static int empeg_write_room (struct usb_serial_port *port);
  82. static int empeg_chars_in_buffer (struct usb_serial_port *port);
  83. static void empeg_throttle (struct usb_serial_port *port);
  84. static void empeg_unthrottle (struct usb_serial_port *port);
  85. static int empeg_startup (struct usb_serial *serial);
  86. static void empeg_shutdown (struct usb_serial *serial);
  87. static int empeg_ioctl (struct usb_serial_port *port,
  88. struct file * file,
  89. unsigned int cmd,
  90. unsigned long arg);
  91. static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios);
  92. static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  93. static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  94. static struct usb_device_id id_table [] = {
  95. { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
  96. { } /* Terminating entry */
  97. };
  98. MODULE_DEVICE_TABLE (usb, id_table);
  99. static struct usb_driver empeg_driver = {
  100. .owner = THIS_MODULE,
  101. .name = "empeg",
  102. .probe = usb_serial_probe,
  103. .disconnect = usb_serial_disconnect,
  104. .id_table = id_table,
  105. };
  106. static struct usb_serial_device_type empeg_device = {
  107. .owner = THIS_MODULE,
  108. .name = "Empeg",
  109. .id_table = id_table,
  110. .num_interrupt_in = 0,
  111. .num_bulk_in = 1,
  112. .num_bulk_out = 1,
  113. .num_ports = 1,
  114. .open = empeg_open,
  115. .close = empeg_close,
  116. .throttle = empeg_throttle,
  117. .unthrottle = empeg_unthrottle,
  118. .attach = empeg_startup,
  119. .shutdown = empeg_shutdown,
  120. .ioctl = empeg_ioctl,
  121. .set_termios = empeg_set_termios,
  122. .write = empeg_write,
  123. .write_room = empeg_write_room,
  124. .chars_in_buffer = empeg_chars_in_buffer,
  125. .write_bulk_callback = empeg_write_bulk_callback,
  126. .read_bulk_callback = empeg_read_bulk_callback,
  127. };
  128. #define NUM_URBS 16
  129. #define URB_TRANSFER_BUFFER_SIZE 4096
  130. static struct urb *write_urb_pool[NUM_URBS];
  131. static spinlock_t write_urb_pool_lock;
  132. static int bytes_in;
  133. static int bytes_out;
  134. /******************************************************************************
  135. * Empeg specific driver functions
  136. ******************************************************************************/
  137. static int empeg_open (struct usb_serial_port *port, struct file *filp)
  138. {
  139. struct usb_serial *serial = port->serial;
  140. int result = 0;
  141. dbg("%s - port %d", __FUNCTION__, port->number);
  142. /* Force default termio settings */
  143. empeg_set_termios (port, NULL) ;
  144. bytes_in = 0;
  145. bytes_out = 0;
  146. /* Start reading from the device */
  147. usb_fill_bulk_urb(
  148. port->read_urb,
  149. serial->dev,
  150. usb_rcvbulkpipe(serial->dev,
  151. port->bulk_in_endpointAddress),
  152. port->read_urb->transfer_buffer,
  153. port->read_urb->transfer_buffer_length,
  154. empeg_read_bulk_callback,
  155. port);
  156. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  157. if (result)
  158. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  159. return result;
  160. }
  161. static void empeg_close (struct usb_serial_port *port, struct file * filp)
  162. {
  163. dbg("%s - port %d", __FUNCTION__, port->number);
  164. /* shutdown our bulk read */
  165. usb_kill_urb(port->read_urb);
  166. /* Uncomment the following line if you want to see some statistics in your syslog */
  167. /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
  168. }
  169. static int empeg_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  170. {
  171. struct usb_serial *serial = port->serial;
  172. struct urb *urb;
  173. const unsigned char *current_position = buf;
  174. unsigned long flags;
  175. int status;
  176. int i;
  177. int bytes_sent = 0;
  178. int transfer_size;
  179. dbg("%s - port %d", __FUNCTION__, port->number);
  180. while (count > 0) {
  181. /* try to find a free urb in our list of them */
  182. urb = NULL;
  183. spin_lock_irqsave (&write_urb_pool_lock, flags);
  184. for (i = 0; i < NUM_URBS; ++i) {
  185. if (write_urb_pool[i]->status != -EINPROGRESS) {
  186. urb = write_urb_pool[i];
  187. break;
  188. }
  189. }
  190. spin_unlock_irqrestore (&write_urb_pool_lock, flags);
  191. if (urb == NULL) {
  192. dbg("%s - no more free urbs", __FUNCTION__);
  193. goto exit;
  194. }
  195. if (urb->transfer_buffer == NULL) {
  196. urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
  197. if (urb->transfer_buffer == NULL) {
  198. dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
  199. goto exit;
  200. }
  201. }
  202. transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
  203. memcpy (urb->transfer_buffer, current_position, transfer_size);
  204. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size, urb->transfer_buffer);
  205. /* build up our urb */
  206. usb_fill_bulk_urb (
  207. urb,
  208. serial->dev,
  209. usb_sndbulkpipe(serial->dev,
  210. port->bulk_out_endpointAddress),
  211. urb->transfer_buffer,
  212. transfer_size,
  213. empeg_write_bulk_callback,
  214. port);
  215. /* send it down the pipe */
  216. status = usb_submit_urb(urb, GFP_ATOMIC);
  217. if (status) {
  218. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
  219. bytes_sent = status;
  220. break;
  221. }
  222. current_position += transfer_size;
  223. bytes_sent += transfer_size;
  224. count -= transfer_size;
  225. bytes_out += transfer_size;
  226. }
  227. exit:
  228. return bytes_sent;
  229. }
  230. static int empeg_write_room (struct usb_serial_port *port)
  231. {
  232. unsigned long flags;
  233. int i;
  234. int room = 0;
  235. dbg("%s - port %d", __FUNCTION__, port->number);
  236. spin_lock_irqsave (&write_urb_pool_lock, flags);
  237. /* tally up the number of bytes available */
  238. for (i = 0; i < NUM_URBS; ++i) {
  239. if (write_urb_pool[i]->status != -EINPROGRESS) {
  240. room += URB_TRANSFER_BUFFER_SIZE;
  241. }
  242. }
  243. spin_unlock_irqrestore (&write_urb_pool_lock, flags);
  244. dbg("%s - returns %d", __FUNCTION__, room);
  245. return (room);
  246. }
  247. static int empeg_chars_in_buffer (struct usb_serial_port *port)
  248. {
  249. unsigned long flags;
  250. int i;
  251. int chars = 0;
  252. dbg("%s - port %d", __FUNCTION__, port->number);
  253. spin_lock_irqsave (&write_urb_pool_lock, flags);
  254. /* tally up the number of bytes waiting */
  255. for (i = 0; i < NUM_URBS; ++i) {
  256. if (write_urb_pool[i]->status == -EINPROGRESS) {
  257. chars += URB_TRANSFER_BUFFER_SIZE;
  258. }
  259. }
  260. spin_unlock_irqrestore (&write_urb_pool_lock, flags);
  261. dbg("%s - returns %d", __FUNCTION__, chars);
  262. return (chars);
  263. }
  264. static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  265. {
  266. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  267. dbg("%s - port %d", __FUNCTION__, port->number);
  268. if (urb->status) {
  269. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  270. return;
  271. }
  272. schedule_work(&port->work);
  273. }
  274. static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  275. {
  276. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  277. struct tty_struct *tty;
  278. unsigned char *data = urb->transfer_buffer;
  279. int i;
  280. int result;
  281. dbg("%s - port %d", __FUNCTION__, port->number);
  282. if (urb->status) {
  283. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  284. return;
  285. }
  286. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  287. tty = port->tty;
  288. if (urb->actual_length) {
  289. for (i = 0; i < urb->actual_length ; ++i) {
  290. /* gb - 2000/11/13
  291. * If we insert too many characters we'll overflow the buffer.
  292. * This means we'll lose bytes - Decidedly bad.
  293. */
  294. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  295. tty_flip_buffer_push(tty);
  296. }
  297. tty_insert_flip_char(tty, data[i], 0);
  298. }
  299. /* gb - 2000/11/13
  300. * Goes straight through instead of scheduling - if tty->low_latency is set.
  301. */
  302. tty_flip_buffer_push(tty);
  303. bytes_in += urb->actual_length;
  304. }
  305. /* Continue trying to always read */
  306. usb_fill_bulk_urb(
  307. port->read_urb,
  308. port->serial->dev,
  309. usb_rcvbulkpipe(port->serial->dev,
  310. port->bulk_in_endpointAddress),
  311. port->read_urb->transfer_buffer,
  312. port->read_urb->transfer_buffer_length,
  313. empeg_read_bulk_callback,
  314. port);
  315. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  316. if (result)
  317. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  318. return;
  319. }
  320. static void empeg_throttle (struct usb_serial_port *port)
  321. {
  322. dbg("%s - port %d", __FUNCTION__, port->number);
  323. usb_kill_urb(port->read_urb);
  324. }
  325. static void empeg_unthrottle (struct usb_serial_port *port)
  326. {
  327. int result;
  328. dbg("%s - port %d", __FUNCTION__, port->number);
  329. port->read_urb->dev = port->serial->dev;
  330. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  331. if (result)
  332. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  333. return;
  334. }
  335. static int empeg_startup (struct usb_serial *serial)
  336. {
  337. int r;
  338. dbg("%s", __FUNCTION__);
  339. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  340. err("active config #%d != 1 ??",
  341. serial->dev->actconfig->desc.bConfigurationValue);
  342. return -ENODEV;
  343. }
  344. dbg("%s - reset config", __FUNCTION__);
  345. r = usb_reset_configuration (serial->dev);
  346. /* continue on with initialization */
  347. return r;
  348. }
  349. static void empeg_shutdown (struct usb_serial *serial)
  350. {
  351. dbg ("%s", __FUNCTION__);
  352. }
  353. static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  354. {
  355. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  356. return -ENOIOCTLCMD;
  357. }
  358. static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  359. {
  360. dbg("%s - port %d", __FUNCTION__, port->number);
  361. if ((!port->tty) || (!port->tty->termios)) {
  362. dbg("%s - no tty structures", __FUNCTION__);
  363. return;
  364. }
  365. /*
  366. * The empeg-car player wants these particular tty settings.
  367. * You could, for example, change the baud rate, however the
  368. * player only supports 115200 (currently), so there is really
  369. * no point in support for changes to the tty settings.
  370. * (at least for now)
  371. *
  372. * The default requirements for this device are:
  373. */
  374. port->tty->termios->c_iflag
  375. &= ~(IGNBRK /* disable ignore break */
  376. | BRKINT /* disable break causes interrupt */
  377. | PARMRK /* disable mark parity errors */
  378. | ISTRIP /* disable clear high bit of input characters */
  379. | INLCR /* disable translate NL to CR */
  380. | IGNCR /* disable ignore CR */
  381. | ICRNL /* disable translate CR to NL */
  382. | IXON); /* disable enable XON/XOFF flow control */
  383. port->tty->termios->c_oflag
  384. &= ~OPOST; /* disable postprocess output characters */
  385. port->tty->termios->c_lflag
  386. &= ~(ECHO /* disable echo input characters */
  387. | ECHONL /* disable echo new line */
  388. | ICANON /* disable erase, kill, werase, and rprnt special characters */
  389. | ISIG /* disable interrupt, quit, and suspend special characters */
  390. | IEXTEN); /* disable non-POSIX special characters */
  391. port->tty->termios->c_cflag
  392. &= ~(CSIZE /* no size */
  393. | PARENB /* disable parity bit */
  394. | CBAUD); /* clear current baud rate */
  395. port->tty->termios->c_cflag
  396. |= (CS8 /* character size 8 bits */
  397. | B115200); /* baud rate 115200 */
  398. /*
  399. * Force low_latency on; otherwise the pushes are scheduled;
  400. * this is bad as it opens up the possibility of dropping bytes
  401. * on the floor. We don't want to drop bytes on the floor. :)
  402. */
  403. port->tty->low_latency = 1;
  404. return;
  405. }
  406. static int __init empeg_init (void)
  407. {
  408. struct urb *urb;
  409. int i, retval;
  410. /* create our write urb pool and transfer buffers */
  411. spin_lock_init (&write_urb_pool_lock);
  412. for (i = 0; i < NUM_URBS; ++i) {
  413. urb = usb_alloc_urb(0, GFP_KERNEL);
  414. write_urb_pool[i] = urb;
  415. if (urb == NULL) {
  416. err("No more urbs???");
  417. continue;
  418. }
  419. urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  420. if (!urb->transfer_buffer) {
  421. err("%s - out of memory for urb buffers.",
  422. __FUNCTION__);
  423. continue;
  424. }
  425. }
  426. retval = usb_serial_register(&empeg_device);
  427. if (retval)
  428. goto failed_usb_serial_register;
  429. retval = usb_register(&empeg_driver);
  430. if (retval)
  431. goto failed_usb_register;
  432. info(DRIVER_VERSION ":" DRIVER_DESC);
  433. return 0;
  434. failed_usb_register:
  435. usb_serial_deregister(&empeg_device);
  436. failed_usb_serial_register:
  437. for (i = 0; i < NUM_URBS; ++i) {
  438. if (write_urb_pool[i]) {
  439. kfree(write_urb_pool[i]->transfer_buffer);
  440. usb_free_urb(write_urb_pool[i]);
  441. }
  442. }
  443. return retval;
  444. }
  445. static void __exit empeg_exit (void)
  446. {
  447. int i;
  448. unsigned long flags;
  449. usb_deregister(&empeg_driver);
  450. usb_serial_deregister (&empeg_device);
  451. spin_lock_irqsave (&write_urb_pool_lock, flags);
  452. for (i = 0; i < NUM_URBS; ++i) {
  453. if (write_urb_pool[i]) {
  454. /* FIXME - uncomment the following usb_kill_urb call when
  455. * the host controllers get fixed to set urb->dev = NULL after
  456. * the urb is finished. Otherwise this call oopses. */
  457. /* usb_kill_urb(write_urb_pool[i]); */
  458. kfree(write_urb_pool[i]->transfer_buffer);
  459. usb_free_urb (write_urb_pool[i]);
  460. }
  461. }
  462. spin_unlock_irqrestore (&write_urb_pool_lock, flags);
  463. }
  464. module_init(empeg_init);
  465. module_exit(empeg_exit);
  466. MODULE_AUTHOR( DRIVER_AUTHOR );
  467. MODULE_DESCRIPTION( DRIVER_DESC );
  468. MODULE_LICENSE("GPL");
  469. module_param(debug, bool, S_IRUGO | S_IWUSR);
  470. MODULE_PARM_DESC(debug, "Debug enabled or not");