empeg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 tty_struct *tty, struct usb_serial_port *port,
  80. struct file *filp);
  81. static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
  82. const unsigned char *buf,
  83. int count);
  84. static int empeg_write_room(struct tty_struct *tty);
  85. static int empeg_chars_in_buffer(struct tty_struct *tty);
  86. static void empeg_throttle(struct tty_struct *tty);
  87. static void empeg_unthrottle(struct tty_struct *tty);
  88. static int empeg_startup(struct usb_serial *serial);
  89. static void empeg_shutdown(struct usb_serial *serial);
  90. static void empeg_set_termios(struct tty_struct *tty,
  91. struct usb_serial_port *port, struct ktermios *old_termios);
  92. static void empeg_write_bulk_callback(struct urb *urb);
  93. static void empeg_read_bulk_callback(struct urb *urb);
  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. .usb_driver = &empeg_driver,
  113. .num_ports = 1,
  114. .open = empeg_open,
  115. .close = empeg_close,
  116. .throttle = empeg_throttle,
  117. .unthrottle = empeg_unthrottle,
  118. .attach = empeg_startup,
  119. .shutdown = empeg_shutdown,
  120. .set_termios = empeg_set_termios,
  121. .write = empeg_write,
  122. .write_room = empeg_write_room,
  123. .chars_in_buffer = empeg_chars_in_buffer,
  124. .write_bulk_callback = empeg_write_bulk_callback,
  125. .read_bulk_callback = empeg_read_bulk_callback,
  126. };
  127. #define NUM_URBS 16
  128. #define URB_TRANSFER_BUFFER_SIZE 4096
  129. static struct urb *write_urb_pool[NUM_URBS];
  130. static spinlock_t write_urb_pool_lock;
  131. static int bytes_in;
  132. static int bytes_out;
  133. /******************************************************************************
  134. * Empeg specific driver functions
  135. ******************************************************************************/
  136. static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
  137. struct file *filp)
  138. {
  139. struct usb_serial *serial = port->serial;
  140. int result = 0;
  141. dbg("%s - port %d", __func__, port->number);
  142. /* Force default termio settings */
  143. empeg_set_termios(tty, port, NULL) ;
  144. bytes_in = 0;
  145. bytes_out = 0;
  146. /* Start reading from the device */
  147. usb_fill_bulk_urb(
  148. port->read_urb,
  149. serial->dev,
  150. usb_rcvbulkpipe(serial->dev,
  151. port->bulk_in_endpointAddress),
  152. port->read_urb->transfer_buffer,
  153. port->read_urb->transfer_buffer_length,
  154. empeg_read_bulk_callback,
  155. port);
  156. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  157. if (result)
  158. dev_err(&port->dev,
  159. "%s - failed submitting read urb, error %d\n",
  160. __func__, result);
  161. return result;
  162. }
  163. static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port,
  164. struct file *filp)
  165. {
  166. dbg("%s - port %d", __func__, port->number);
  167. /* shutdown our bulk read */
  168. usb_kill_urb(port->read_urb);
  169. /* Uncomment the following line if you want to see some statistics in your syslog */
  170. /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
  171. }
  172. static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
  173. const unsigned char *buf, int count)
  174. {
  175. struct usb_serial *serial = port->serial;
  176. struct urb *urb;
  177. const unsigned char *current_position = buf;
  178. unsigned long flags;
  179. int status;
  180. int i;
  181. int bytes_sent = 0;
  182. int transfer_size;
  183. dbg("%s - port %d", __func__, port->number);
  184. while (count > 0) {
  185. /* try to find a free urb in our list of them */
  186. urb = NULL;
  187. spin_lock_irqsave(&write_urb_pool_lock, flags);
  188. for (i = 0; i < NUM_URBS; ++i) {
  189. if (write_urb_pool[i]->status != -EINPROGRESS) {
  190. urb = write_urb_pool[i];
  191. break;
  192. }
  193. }
  194. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  195. if (urb == NULL) {
  196. dbg("%s - no more free urbs", __func__);
  197. goto exit;
  198. }
  199. if (urb->transfer_buffer == NULL) {
  200. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
  201. if (urb->transfer_buffer == NULL) {
  202. dev_err(&port->dev,
  203. "%s no more kernel memory...\n",
  204. __func__);
  205. goto exit;
  206. }
  207. }
  208. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  209. memcpy(urb->transfer_buffer, current_position, transfer_size);
  210. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
  211. /* build up our urb */
  212. usb_fill_bulk_urb(
  213. urb,
  214. serial->dev,
  215. usb_sndbulkpipe(serial->dev,
  216. port->bulk_out_endpointAddress),
  217. urb->transfer_buffer,
  218. transfer_size,
  219. empeg_write_bulk_callback,
  220. port);
  221. /* send it down the pipe */
  222. status = usb_submit_urb(urb, GFP_ATOMIC);
  223. if (status) {
  224. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
  225. bytes_sent = status;
  226. break;
  227. }
  228. current_position += transfer_size;
  229. bytes_sent += transfer_size;
  230. count -= transfer_size;
  231. bytes_out += transfer_size;
  232. }
  233. exit:
  234. return bytes_sent;
  235. }
  236. static int empeg_write_room(struct tty_struct *tty)
  237. {
  238. struct usb_serial_port *port = tty->driver_data;
  239. unsigned long flags;
  240. int i;
  241. int room = 0;
  242. dbg("%s - port %d", __func__, port->number);
  243. spin_lock_irqsave(&write_urb_pool_lock, flags);
  244. /* tally up the number of bytes available */
  245. for (i = 0; i < NUM_URBS; ++i) {
  246. if (write_urb_pool[i]->status != -EINPROGRESS)
  247. room += URB_TRANSFER_BUFFER_SIZE;
  248. }
  249. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  250. dbg("%s - returns %d", __func__, room);
  251. return room;
  252. }
  253. static int empeg_chars_in_buffer(struct tty_struct *tty)
  254. {
  255. struct usb_serial_port *port = tty->driver_data;
  256. unsigned long flags;
  257. int i;
  258. int chars = 0;
  259. dbg("%s - port %d", __func__, port->number);
  260. spin_lock_irqsave(&write_urb_pool_lock, flags);
  261. /* tally up the number of bytes waiting */
  262. for (i = 0; i < NUM_URBS; ++i) {
  263. if (write_urb_pool[i]->status == -EINPROGRESS)
  264. chars += URB_TRANSFER_BUFFER_SIZE;
  265. }
  266. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  267. dbg("%s - returns %d", __func__, chars);
  268. return chars;
  269. }
  270. static void empeg_write_bulk_callback(struct urb *urb)
  271. {
  272. struct usb_serial_port *port = urb->context;
  273. int status = urb->status;
  274. dbg("%s - port %d", __func__, port->number);
  275. if (status) {
  276. dbg("%s - nonzero write bulk status received: %d",
  277. __func__, status);
  278. return;
  279. }
  280. usb_serial_port_softint(port);
  281. }
  282. static void empeg_read_bulk_callback(struct urb *urb)
  283. {
  284. struct usb_serial_port *port = urb->context;
  285. struct tty_struct *tty;
  286. unsigned char *data = urb->transfer_buffer;
  287. int result;
  288. int status = urb->status;
  289. dbg("%s - port %d", __func__, port->number);
  290. if (status) {
  291. dbg("%s - nonzero read bulk status received: %d",
  292. __func__, status);
  293. return;
  294. }
  295. usb_serial_debug_data(debug, &port->dev, __func__,
  296. urb->actual_length, data);
  297. tty = tty_port_tty_get(&port->port);
  298. if (urb->actual_length) {
  299. tty_buffer_request_room(tty, urb->actual_length);
  300. tty_insert_flip_string(tty, data, urb->actual_length);
  301. tty_flip_buffer_push(tty);
  302. bytes_in += urb->actual_length;
  303. }
  304. tty_kref_put(tty);
  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. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  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. printk(KERN_ERR "empeg: No more urbs???\n");
  414. continue;
  415. }
  416. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  417. GFP_KERNEL);
  418. if (!urb->transfer_buffer) {
  419. printk(KERN_ERR "empeg: %s - out of memory for urb "
  420. "buffers.", __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. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  431. DRIVER_DESC "\n");
  432. return 0;
  433. failed_usb_register:
  434. usb_serial_deregister(&empeg_device);
  435. failed_usb_serial_register:
  436. for (i = 0; i < NUM_URBS; ++i) {
  437. if (write_urb_pool[i]) {
  438. kfree(write_urb_pool[i]->transfer_buffer);
  439. usb_free_urb(write_urb_pool[i]);
  440. }
  441. }
  442. return retval;
  443. }
  444. static void __exit empeg_exit(void)
  445. {
  446. int i;
  447. unsigned long flags;
  448. usb_deregister(&empeg_driver);
  449. usb_serial_deregister(&empeg_device);
  450. spin_lock_irqsave(&write_urb_pool_lock, flags);
  451. for (i = 0; i < NUM_URBS; ++i) {
  452. if (write_urb_pool[i]) {
  453. /* FIXME - uncomment the following usb_kill_urb call
  454. * when the host controllers get fixed to set urb->dev
  455. * = NULL after the urb is finished. Otherwise this
  456. * call oopses. */
  457. /* usb_kill_urb(write_urb_pool[i]); */
  458. kfree(write_urb_pool[i]->transfer_buffer);
  459. usb_free_urb(write_urb_pool[i]);
  460. }
  461. }
  462. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  463. }
  464. module_init(empeg_init);
  465. module_exit(empeg_exit);
  466. MODULE_AUTHOR(DRIVER_AUTHOR);
  467. MODULE_DESCRIPTION(DRIVER_DESC);
  468. MODULE_LICENSE("GPL");
  469. module_param(debug, bool, S_IRUGO | S_IWUSR);
  470. MODULE_PARM_DESC(debug, "Debug enabled or not");