empeg.c 15 KB

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