empeg.c 15 KB

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