empeg.c 14 KB

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