empeg.c 15 KB

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