empeg.c 15 KB

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