empeg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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
  15. * driver
  16. *
  17. * (07/16/2001) gb
  18. * remove unused code in empeg_close() (thanks to Oliver Neukum for
  19. * pointing this out) and rewrote empeg_set_termios().
  20. *
  21. * (05/30/2001) gkh
  22. * switched from using spinlock to a semaphore, which fixes lots of
  23. * problems.
  24. *
  25. * (04/08/2001) gb
  26. * Identify version on module load.
  27. *
  28. * (01/22/2001) gb
  29. * Added write_room() and chars_in_buffer() support.
  30. *
  31. * (12/21/2000) gb
  32. * Moved termio stuff inside the port->active check.
  33. * Moved MOD_DEC_USE_COUNT to end of empeg_close().
  34. *
  35. * (12/03/2000) gb
  36. * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
  37. * This notifies the tty driver that the termios have changed.
  38. *
  39. * (11/13/2000) gb
  40. * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
  41. * empeg_open() (It only needs to be set once - Doh!)
  42. *
  43. * (11/11/2000) gb
  44. * Updated to work with id_table structure.
  45. *
  46. * (11/04/2000) gb
  47. * Forked this from visor.c, and hacked it up to work with an
  48. * Empeg ltd. empeg-car player. Constructive criticism welcomed.
  49. * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
  50. * use of his code, and for his guidance, advice and patience. :)
  51. * A 'Thank You' is in order for John Ripley of Empeg ltd for his
  52. * advice, and patience too.
  53. *
  54. */
  55. #include <linux/kernel.h>
  56. #include <linux/errno.h>
  57. #include <linux/init.h>
  58. #include <linux/slab.h>
  59. #include <linux/tty.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/module.h>
  63. #include <linux/spinlock.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/usb.h>
  66. #include <linux/usb/serial.h>
  67. static int debug;
  68. /*
  69. * Version Information
  70. */
  71. #define DRIVER_VERSION "v1.2"
  72. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
  73. #define DRIVER_DESC "USB Empeg Mark I/II Driver"
  74. #define EMPEG_VENDOR_ID 0x084f
  75. #define EMPEG_PRODUCT_ID 0x0001
  76. /* function prototypes for an empeg-car player */
  77. static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
  78. struct file *filp);
  79. static void empeg_close(struct usb_serial_port *port);
  80. static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
  81. const unsigned char *buf,
  82. int count);
  83. static int empeg_write_room(struct tty_struct *tty);
  84. static int empeg_chars_in_buffer(struct tty_struct *tty);
  85. static void empeg_throttle(struct tty_struct *tty);
  86. static void empeg_unthrottle(struct tty_struct *tty);
  87. static int empeg_startup(struct usb_serial *serial);
  88. static void empeg_set_termios(struct tty_struct *tty,
  89. struct usb_serial_port *port, struct ktermios *old_termios);
  90. static void empeg_write_bulk_callback(struct urb *urb);
  91. static void empeg_read_bulk_callback(struct urb *urb);
  92. static struct usb_device_id id_table [] = {
  93. { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
  94. { } /* Terminating entry */
  95. };
  96. MODULE_DEVICE_TABLE(usb, id_table);
  97. static struct usb_driver empeg_driver = {
  98. .name = "empeg",
  99. .probe = usb_serial_probe,
  100. .disconnect = usb_serial_disconnect,
  101. .id_table = id_table,
  102. .no_dynamic_id = 1,
  103. };
  104. static struct usb_serial_driver empeg_device = {
  105. .driver = {
  106. .owner = THIS_MODULE,
  107. .name = "empeg",
  108. },
  109. .id_table = id_table,
  110. .usb_driver = &empeg_driver,
  111. .num_ports = 1,
  112. .open = empeg_open,
  113. .close = empeg_close,
  114. .throttle = empeg_throttle,
  115. .unthrottle = empeg_unthrottle,
  116. .attach = empeg_startup,
  117. .set_termios = empeg_set_termios,
  118. .write = empeg_write,
  119. .write_room = empeg_write_room,
  120. .chars_in_buffer = empeg_chars_in_buffer,
  121. .write_bulk_callback = empeg_write_bulk_callback,
  122. .read_bulk_callback = empeg_read_bulk_callback,
  123. };
  124. #define NUM_URBS 16
  125. #define URB_TRANSFER_BUFFER_SIZE 4096
  126. static struct urb *write_urb_pool[NUM_URBS];
  127. static spinlock_t write_urb_pool_lock;
  128. static int bytes_in;
  129. static int bytes_out;
  130. /******************************************************************************
  131. * Empeg specific driver functions
  132. ******************************************************************************/
  133. static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
  134. struct file *filp)
  135. {
  136. struct usb_serial *serial = port->serial;
  137. int result = 0;
  138. dbg("%s - port %d", __func__, port->number);
  139. /* Force default termio settings */
  140. empeg_set_termios(tty, port, NULL) ;
  141. bytes_in = 0;
  142. bytes_out = 0;
  143. /* Start reading from the device */
  144. usb_fill_bulk_urb(
  145. port->read_urb,
  146. serial->dev,
  147. usb_rcvbulkpipe(serial->dev,
  148. port->bulk_in_endpointAddress),
  149. port->read_urb->transfer_buffer,
  150. port->read_urb->transfer_buffer_length,
  151. empeg_read_bulk_callback,
  152. port);
  153. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  154. if (result)
  155. dev_err(&port->dev,
  156. "%s - failed submitting read urb, error %d\n",
  157. __func__, result);
  158. return result;
  159. }
  160. static void empeg_close(struct usb_serial_port *port)
  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 tty_struct *tty, struct usb_serial_port *port,
  169. 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", __func__, 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", __func__);
  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,
  199. "%s no more kernel memory...\n",
  200. __func__);
  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, __func__, 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", __func__, 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 tty_struct *tty)
  233. {
  234. struct usb_serial_port *port = tty->driver_data;
  235. unsigned long flags;
  236. int i;
  237. int room = 0;
  238. dbg("%s - port %d", __func__, port->number);
  239. spin_lock_irqsave(&write_urb_pool_lock, flags);
  240. /* tally up the number of bytes available */
  241. for (i = 0; i < NUM_URBS; ++i) {
  242. if (write_urb_pool[i]->status != -EINPROGRESS)
  243. room += URB_TRANSFER_BUFFER_SIZE;
  244. }
  245. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  246. dbg("%s - returns %d", __func__, room);
  247. return room;
  248. }
  249. static int empeg_chars_in_buffer(struct tty_struct *tty)
  250. {
  251. struct usb_serial_port *port = tty->driver_data;
  252. unsigned long flags;
  253. int i;
  254. int chars = 0;
  255. dbg("%s - port %d", __func__, port->number);
  256. spin_lock_irqsave(&write_urb_pool_lock, flags);
  257. /* tally up the number of bytes waiting */
  258. for (i = 0; i < NUM_URBS; ++i) {
  259. if (write_urb_pool[i]->status == -EINPROGRESS)
  260. chars += URB_TRANSFER_BUFFER_SIZE;
  261. }
  262. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  263. dbg("%s - returns %d", __func__, chars);
  264. return chars;
  265. }
  266. static void empeg_write_bulk_callback(struct urb *urb)
  267. {
  268. struct usb_serial_port *port = urb->context;
  269. int status = urb->status;
  270. dbg("%s - port %d", __func__, port->number);
  271. if (status) {
  272. dbg("%s - nonzero write bulk status received: %d",
  273. __func__, status);
  274. return;
  275. }
  276. usb_serial_port_softint(port);
  277. }
  278. static void empeg_read_bulk_callback(struct urb *urb)
  279. {
  280. struct usb_serial_port *port = urb->context;
  281. struct tty_struct *tty;
  282. unsigned char *data = urb->transfer_buffer;
  283. int result;
  284. int status = urb->status;
  285. dbg("%s - port %d", __func__, port->number);
  286. if (status) {
  287. dbg("%s - nonzero read bulk status received: %d",
  288. __func__, status);
  289. return;
  290. }
  291. usb_serial_debug_data(debug, &port->dev, __func__,
  292. urb->actual_length, data);
  293. tty = tty_port_tty_get(&port->port);
  294. if (urb->actual_length) {
  295. tty_buffer_request_room(tty, urb->actual_length);
  296. tty_insert_flip_string(tty, data, urb->actual_length);
  297. tty_flip_buffer_push(tty);
  298. bytes_in += urb->actual_length;
  299. }
  300. tty_kref_put(tty);
  301. /* Continue trying to always read */
  302. usb_fill_bulk_urb(
  303. port->read_urb,
  304. port->serial->dev,
  305. usb_rcvbulkpipe(port->serial->dev,
  306. port->bulk_in_endpointAddress),
  307. port->read_urb->transfer_buffer,
  308. port->read_urb->transfer_buffer_length,
  309. empeg_read_bulk_callback,
  310. port);
  311. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  312. if (result)
  313. dev_err(&urb->dev->dev,
  314. "%s - failed resubmitting read urb, error %d\n",
  315. __func__, result);
  316. return;
  317. }
  318. static void empeg_throttle(struct tty_struct *tty)
  319. {
  320. struct usb_serial_port *port = tty->driver_data;
  321. dbg("%s - port %d", __func__, port->number);
  322. usb_kill_urb(port->read_urb);
  323. }
  324. static void empeg_unthrottle(struct tty_struct *tty)
  325. {
  326. struct usb_serial_port *port = tty->driver_data;
  327. int result;
  328. dbg("%s - port %d", __func__, 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,
  333. "%s - failed submitting read urb, error %d\n",
  334. __func__, result);
  335. }
  336. static int empeg_startup(struct usb_serial *serial)
  337. {
  338. int r;
  339. dbg("%s", __func__);
  340. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  341. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  342. serial->dev->actconfig->desc.bConfigurationValue);
  343. return -ENODEV;
  344. }
  345. dbg("%s - reset config", __func__);
  346. r = usb_reset_configuration(serial->dev);
  347. /* continue on with initialization */
  348. return r;
  349. }
  350. static void empeg_set_termios(struct tty_struct *tty,
  351. struct usb_serial_port *port, struct ktermios *old_termios)
  352. {
  353. struct ktermios *termios = tty->termios;
  354. dbg("%s - port %d", __func__, port->number);
  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. 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. termios->c_oflag
  374. &= ~OPOST; /* disable postprocess output characters */
  375. 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. termios->c_cflag
  382. &= ~(CSIZE /* no size */
  383. | PARENB /* disable parity bit */
  384. | CBAUD); /* clear current baud rate */
  385. termios->c_cflag
  386. |= CS8; /* character size 8 bits */
  387. tty_encode_baud_rate(tty, 115200, 115200);
  388. }
  389. static int __init empeg_init(void)
  390. {
  391. struct urb *urb;
  392. int i, retval;
  393. /* create our write urb pool and transfer buffers */
  394. spin_lock_init(&write_urb_pool_lock);
  395. for (i = 0; i < NUM_URBS; ++i) {
  396. urb = usb_alloc_urb(0, GFP_KERNEL);
  397. write_urb_pool[i] = urb;
  398. if (urb == NULL) {
  399. printk(KERN_ERR "empeg: No more urbs???\n");
  400. continue;
  401. }
  402. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  403. GFP_KERNEL);
  404. if (!urb->transfer_buffer) {
  405. printk(KERN_ERR "empeg: %s - out of memory for urb "
  406. "buffers.", __func__);
  407. continue;
  408. }
  409. }
  410. retval = usb_serial_register(&empeg_device);
  411. if (retval)
  412. goto failed_usb_serial_register;
  413. retval = usb_register(&empeg_driver);
  414. if (retval)
  415. goto failed_usb_register;
  416. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  417. DRIVER_DESC "\n");
  418. return 0;
  419. failed_usb_register:
  420. usb_serial_deregister(&empeg_device);
  421. failed_usb_serial_register:
  422. for (i = 0; i < NUM_URBS; ++i) {
  423. if (write_urb_pool[i]) {
  424. kfree(write_urb_pool[i]->transfer_buffer);
  425. usb_free_urb(write_urb_pool[i]);
  426. }
  427. }
  428. return retval;
  429. }
  430. static void __exit empeg_exit(void)
  431. {
  432. int i;
  433. unsigned long flags;
  434. usb_deregister(&empeg_driver);
  435. usb_serial_deregister(&empeg_device);
  436. spin_lock_irqsave(&write_urb_pool_lock, flags);
  437. for (i = 0; i < NUM_URBS; ++i) {
  438. if (write_urb_pool[i]) {
  439. /* FIXME - uncomment the following usb_kill_urb call
  440. * when the host controllers get fixed to set urb->dev
  441. * = NULL after the urb is finished. Otherwise this
  442. * call oopses. */
  443. /* usb_kill_urb(write_urb_pool[i]); */
  444. kfree(write_urb_pool[i]->transfer_buffer);
  445. usb_free_urb(write_urb_pool[i]);
  446. }
  447. }
  448. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  449. }
  450. module_init(empeg_init);
  451. module_exit(empeg_exit);
  452. MODULE_AUTHOR(DRIVER_AUTHOR);
  453. MODULE_DESCRIPTION(DRIVER_DESC);
  454. MODULE_LICENSE("GPL");
  455. module_param(debug, bool, S_IRUGO | S_IWUSR);
  456. MODULE_PARM_DESC(debug, "Debug enabled or not");