visor.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this
  13. * driver
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port,
  37. struct file *filp);
  38. static void visor_close(struct tty_struct *tty, struct usb_serial_port *port,
  39. struct file *filp);
  40. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  41. const unsigned char *buf, int count);
  42. static int visor_write_room(struct tty_struct *tty);
  43. static void visor_throttle(struct tty_struct *tty);
  44. static void visor_unthrottle(struct tty_struct *tty);
  45. static int visor_probe(struct usb_serial *serial,
  46. const struct usb_device_id *id);
  47. static int visor_calc_num_ports(struct usb_serial *serial);
  48. static void visor_shutdown(struct usb_serial *serial);
  49. static void visor_write_bulk_callback(struct urb *urb);
  50. static void visor_read_bulk_callback(struct urb *urb);
  51. static void visor_read_int_callback(struct urb *urb);
  52. static int clie_3_5_startup(struct usb_serial *serial);
  53. static int treo_attach(struct usb_serial *serial);
  54. static int clie_5_attach(struct usb_serial *serial);
  55. static int palm_os_3_probe(struct usb_serial *serial,
  56. const struct usb_device_id *id);
  57. static int palm_os_4_probe(struct usb_serial *serial,
  58. const struct usb_device_id *id);
  59. /* Parameters that may be passed into the module. */
  60. static int debug;
  61. static __u16 vendor;
  62. static __u16 product;
  63. static struct usb_device_id id_table [] = {
  64. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  65. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  66. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  67. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  68. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  69. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  70. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  71. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  72. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  73. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  74. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  75. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  76. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  77. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  78. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  79. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  80. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  81. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  82. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  83. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  84. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  85. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  86. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  87. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  88. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  89. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  90. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  91. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  92. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  93. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  94. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  95. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  96. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  97. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  98. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  99. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  100. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  101. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  102. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  103. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  104. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  105. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  106. { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
  107. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  108. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  109. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  110. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  111. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  112. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  113. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  114. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  115. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  116. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  117. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  118. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  119. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  120. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  121. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  122. { }, /* optional parameter entry */
  123. { } /* Terminating entry */
  124. };
  125. static struct usb_device_id clie_id_5_table [] = {
  126. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  127. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  128. { }, /* optional parameter entry */
  129. { } /* Terminating entry */
  130. };
  131. static struct usb_device_id clie_id_3_5_table [] = {
  132. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  133. { } /* Terminating entry */
  134. };
  135. static struct usb_device_id id_table_combined [] = {
  136. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  137. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  138. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  139. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  146. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  147. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  148. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  149. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  150. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  154. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  155. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  156. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  157. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  158. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  159. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  160. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  161. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  162. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  163. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  164. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  165. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  166. { }, /* optional parameter entry */
  167. { } /* Terminating entry */
  168. };
  169. MODULE_DEVICE_TABLE(usb, id_table_combined);
  170. static struct usb_driver visor_driver = {
  171. .name = "visor",
  172. .probe = usb_serial_probe,
  173. .disconnect = usb_serial_disconnect,
  174. .id_table = id_table_combined,
  175. .no_dynamic_id = 1,
  176. };
  177. /* All of the device info needed for the Handspring Visor,
  178. and Palm 4.0 devices */
  179. static struct usb_serial_driver handspring_device = {
  180. .driver = {
  181. .owner = THIS_MODULE,
  182. .name = "visor",
  183. },
  184. .description = "Handspring Visor / Palm OS",
  185. .usb_driver = &visor_driver,
  186. .id_table = id_table,
  187. .num_ports = 2,
  188. .open = visor_open,
  189. .close = visor_close,
  190. .throttle = visor_throttle,
  191. .unthrottle = visor_unthrottle,
  192. .attach = treo_attach,
  193. .probe = visor_probe,
  194. .calc_num_ports = visor_calc_num_ports,
  195. .shutdown = visor_shutdown,
  196. .write = visor_write,
  197. .write_room = visor_write_room,
  198. .write_bulk_callback = visor_write_bulk_callback,
  199. .read_bulk_callback = visor_read_bulk_callback,
  200. .read_int_callback = visor_read_int_callback,
  201. };
  202. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  203. static struct usb_serial_driver clie_5_device = {
  204. .driver = {
  205. .owner = THIS_MODULE,
  206. .name = "clie_5",
  207. },
  208. .description = "Sony Clie 5.0",
  209. .usb_driver = &visor_driver,
  210. .id_table = clie_id_5_table,
  211. .num_ports = 2,
  212. .open = visor_open,
  213. .close = visor_close,
  214. .throttle = visor_throttle,
  215. .unthrottle = visor_unthrottle,
  216. .attach = clie_5_attach,
  217. .probe = visor_probe,
  218. .calc_num_ports = visor_calc_num_ports,
  219. .shutdown = visor_shutdown,
  220. .write = visor_write,
  221. .write_room = visor_write_room,
  222. .write_bulk_callback = visor_write_bulk_callback,
  223. .read_bulk_callback = visor_read_bulk_callback,
  224. .read_int_callback = visor_read_int_callback,
  225. };
  226. /* device info for the Sony Clie OS version 3.5 */
  227. static struct usb_serial_driver clie_3_5_device = {
  228. .driver = {
  229. .owner = THIS_MODULE,
  230. .name = "clie_3.5",
  231. },
  232. .description = "Sony Clie 3.5",
  233. .usb_driver = &visor_driver,
  234. .id_table = clie_id_3_5_table,
  235. .num_ports = 1,
  236. .open = visor_open,
  237. .close = visor_close,
  238. .throttle = visor_throttle,
  239. .unthrottle = visor_unthrottle,
  240. .attach = clie_3_5_startup,
  241. .write = visor_write,
  242. .write_room = visor_write_room,
  243. .write_bulk_callback = visor_write_bulk_callback,
  244. .read_bulk_callback = visor_read_bulk_callback,
  245. };
  246. struct visor_private {
  247. spinlock_t lock;
  248. int bytes_in;
  249. int bytes_out;
  250. int outstanding_urbs;
  251. unsigned char throttled;
  252. unsigned char actually_throttled;
  253. };
  254. /* number of outstanding urbs to prevent userspace DoS from happening */
  255. #define URB_UPPER_LIMIT 42
  256. static int stats;
  257. /******************************************************************************
  258. * Handspring Visor specific driver functions
  259. ******************************************************************************/
  260. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port,
  261. struct file *filp)
  262. {
  263. struct usb_serial *serial = port->serial;
  264. struct visor_private *priv = usb_get_serial_port_data(port);
  265. unsigned long flags;
  266. int result = 0;
  267. dbg("%s - port %d", __func__, port->number);
  268. if (!port->read_urb) {
  269. /* this is needed for some brain dead Sony devices */
  270. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  271. return -ENODEV;
  272. }
  273. spin_lock_irqsave(&priv->lock, flags);
  274. priv->bytes_in = 0;
  275. priv->bytes_out = 0;
  276. priv->throttled = 0;
  277. spin_unlock_irqrestore(&priv->lock, flags);
  278. /*
  279. * Force low_latency on so that our tty_push actually forces the data
  280. * through, otherwise it is scheduled, and with high data rates (like
  281. * with OHCI) data can get lost.
  282. */
  283. if (tty)
  284. tty->low_latency = 1;
  285. /* Start reading from the device */
  286. usb_fill_bulk_urb(port->read_urb, serial->dev,
  287. usb_rcvbulkpipe(serial->dev,
  288. port->bulk_in_endpointAddress),
  289. port->read_urb->transfer_buffer,
  290. port->read_urb->transfer_buffer_length,
  291. visor_read_bulk_callback, port);
  292. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  293. if (result) {
  294. dev_err(&port->dev,
  295. "%s - failed submitting read urb, error %d\n",
  296. __func__, result);
  297. goto exit;
  298. }
  299. if (port->interrupt_in_urb) {
  300. dbg("%s - adding interrupt input for treo", __func__);
  301. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  302. if (result)
  303. dev_err(&port->dev,
  304. "%s - failed submitting interrupt urb, error %d\n",
  305. __func__, result);
  306. }
  307. exit:
  308. return result;
  309. }
  310. static void visor_close(struct tty_struct *tty,
  311. struct usb_serial_port *port, struct file *filp)
  312. {
  313. struct visor_private *priv = usb_get_serial_port_data(port);
  314. unsigned char *transfer_buffer;
  315. dbg("%s - port %d", __func__, port->number);
  316. /* shutdown our urbs */
  317. usb_kill_urb(port->read_urb);
  318. usb_kill_urb(port->interrupt_in_urb);
  319. mutex_lock(&port->serial->disc_mutex);
  320. if (!port->serial->disconnected) {
  321. /* Try to send shutdown message, unless the device is gone */
  322. transfer_buffer = kmalloc(0x12, GFP_KERNEL);
  323. if (transfer_buffer) {
  324. usb_control_msg(port->serial->dev,
  325. usb_rcvctrlpipe(port->serial->dev, 0),
  326. VISOR_CLOSE_NOTIFICATION, 0xc2,
  327. 0x0000, 0x0000,
  328. transfer_buffer, 0x12, 300);
  329. kfree(transfer_buffer);
  330. }
  331. }
  332. mutex_unlock(&port->serial->disc_mutex);
  333. if (stats)
  334. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  335. priv->bytes_in, priv->bytes_out);
  336. }
  337. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  338. const unsigned char *buf, int count)
  339. {
  340. struct visor_private *priv = usb_get_serial_port_data(port);
  341. struct usb_serial *serial = port->serial;
  342. struct urb *urb;
  343. unsigned char *buffer;
  344. unsigned long flags;
  345. int status;
  346. dbg("%s - port %d", __func__, port->number);
  347. spin_lock_irqsave(&priv->lock, flags);
  348. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  349. spin_unlock_irqrestore(&priv->lock, flags);
  350. dbg("%s - write limit hit\n", __func__);
  351. return 0;
  352. }
  353. priv->outstanding_urbs++;
  354. spin_unlock_irqrestore(&priv->lock, flags);
  355. buffer = kmalloc(count, GFP_ATOMIC);
  356. if (!buffer) {
  357. dev_err(&port->dev, "out of memory\n");
  358. count = -ENOMEM;
  359. goto error_no_buffer;
  360. }
  361. urb = usb_alloc_urb(0, GFP_ATOMIC);
  362. if (!urb) {
  363. dev_err(&port->dev, "no more free urbs\n");
  364. count = -ENOMEM;
  365. goto error_no_urb;
  366. }
  367. memcpy(buffer, buf, count);
  368. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  369. usb_fill_bulk_urb(urb, serial->dev,
  370. usb_sndbulkpipe(serial->dev,
  371. port->bulk_out_endpointAddress),
  372. buffer, count,
  373. visor_write_bulk_callback, port);
  374. /* send it down the pipe */
  375. status = usb_submit_urb(urb, GFP_ATOMIC);
  376. if (status) {
  377. dev_err(&port->dev,
  378. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  379. __func__, status);
  380. count = status;
  381. goto error;
  382. } else {
  383. spin_lock_irqsave(&priv->lock, flags);
  384. priv->bytes_out += count;
  385. spin_unlock_irqrestore(&priv->lock, flags);
  386. }
  387. /* we are done with this urb, so let the host driver
  388. * really free it when it is finished with it */
  389. usb_free_urb(urb);
  390. return count;
  391. error:
  392. usb_free_urb(urb);
  393. error_no_urb:
  394. kfree(buffer);
  395. error_no_buffer:
  396. spin_lock_irqsave(&priv->lock, flags);
  397. --priv->outstanding_urbs;
  398. spin_unlock_irqrestore(&priv->lock, flags);
  399. return count;
  400. }
  401. static int visor_write_room(struct tty_struct *tty)
  402. {
  403. struct usb_serial_port *port = tty->driver_data;
  404. struct visor_private *priv = usb_get_serial_port_data(port);
  405. unsigned long flags;
  406. dbg("%s - port %d", __func__, port->number);
  407. /*
  408. * We really can take anything the user throws at us
  409. * but let's pick a nice big number to tell the tty
  410. * layer that we have lots of free space, unless we don't.
  411. */
  412. spin_lock_irqsave(&priv->lock, flags);
  413. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  414. spin_unlock_irqrestore(&priv->lock, flags);
  415. dbg("%s - write limit hit\n", __func__);
  416. return 0;
  417. }
  418. spin_unlock_irqrestore(&priv->lock, flags);
  419. return 2048;
  420. }
  421. static void visor_write_bulk_callback(struct urb *urb)
  422. {
  423. struct usb_serial_port *port = urb->context;
  424. struct visor_private *priv = usb_get_serial_port_data(port);
  425. int status = urb->status;
  426. unsigned long flags;
  427. /* free up the transfer buffer, as usb_free_urb() does not do this */
  428. kfree(urb->transfer_buffer);
  429. dbg("%s - port %d", __func__, port->number);
  430. if (status)
  431. dbg("%s - nonzero write bulk status received: %d",
  432. __func__, status);
  433. spin_lock_irqsave(&priv->lock, flags);
  434. --priv->outstanding_urbs;
  435. spin_unlock_irqrestore(&priv->lock, flags);
  436. usb_serial_port_softint(port);
  437. }
  438. static void visor_read_bulk_callback(struct urb *urb)
  439. {
  440. struct usb_serial_port *port = urb->context;
  441. struct visor_private *priv = usb_get_serial_port_data(port);
  442. unsigned char *data = urb->transfer_buffer;
  443. int status = urb->status;
  444. struct tty_struct *tty;
  445. int result;
  446. int available_room;
  447. dbg("%s - port %d", __func__, port->number);
  448. if (status) {
  449. dbg("%s - nonzero read bulk status received: %d",
  450. __func__, status);
  451. return;
  452. }
  453. usb_serial_debug_data(debug, &port->dev, __func__,
  454. urb->actual_length, data);
  455. tty = port->port.tty;
  456. if (tty && urb->actual_length) {
  457. available_room = tty_buffer_request_room(tty,
  458. urb->actual_length);
  459. if (available_room) {
  460. tty_insert_flip_string(tty, data, available_room);
  461. tty_flip_buffer_push(tty);
  462. }
  463. spin_lock(&priv->lock);
  464. priv->bytes_in += available_room;
  465. } else {
  466. spin_lock(&priv->lock);
  467. }
  468. /* Continue trying to always read if we should */
  469. if (!priv->throttled) {
  470. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  471. usb_rcvbulkpipe(port->serial->dev,
  472. port->bulk_in_endpointAddress),
  473. port->read_urb->transfer_buffer,
  474. port->read_urb->transfer_buffer_length,
  475. visor_read_bulk_callback, port);
  476. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  477. if (result)
  478. dev_err(&port->dev,
  479. "%s - failed resubmitting read urb, error %d\n",
  480. __func__, result);
  481. } else
  482. priv->actually_throttled = 1;
  483. spin_unlock(&priv->lock);
  484. }
  485. static void visor_read_int_callback(struct urb *urb)
  486. {
  487. struct usb_serial_port *port = urb->context;
  488. int status = urb->status;
  489. int result;
  490. switch (status) {
  491. case 0:
  492. /* success */
  493. break;
  494. case -ECONNRESET:
  495. case -ENOENT:
  496. case -ESHUTDOWN:
  497. /* this urb is terminated, clean up */
  498. dbg("%s - urb shutting down with status: %d",
  499. __func__, status);
  500. return;
  501. default:
  502. dbg("%s - nonzero urb status received: %d",
  503. __func__, status);
  504. goto exit;
  505. }
  506. /*
  507. * This information is still unknown what it can be used for.
  508. * If anyone has an idea, please let the author know...
  509. *
  510. * Rumor has it this endpoint is used to notify when data
  511. * is ready to be read from the bulk ones.
  512. */
  513. usb_serial_debug_data(debug, &port->dev, __func__,
  514. urb->actual_length, urb->transfer_buffer);
  515. exit:
  516. result = usb_submit_urb(urb, GFP_ATOMIC);
  517. if (result)
  518. dev_err(&urb->dev->dev,
  519. "%s - Error %d submitting interrupt urb\n",
  520. __func__, result);
  521. }
  522. static void visor_throttle(struct tty_struct *tty)
  523. {
  524. struct usb_serial_port *port = tty->driver_data;
  525. struct visor_private *priv = usb_get_serial_port_data(port);
  526. unsigned long flags;
  527. dbg("%s - port %d", __func__, port->number);
  528. spin_lock_irqsave(&priv->lock, flags);
  529. priv->throttled = 1;
  530. spin_unlock_irqrestore(&priv->lock, flags);
  531. }
  532. static void visor_unthrottle(struct tty_struct *tty)
  533. {
  534. struct usb_serial_port *port = tty->driver_data;
  535. struct visor_private *priv = usb_get_serial_port_data(port);
  536. unsigned long flags;
  537. int result;
  538. dbg("%s - port %d", __func__, port->number);
  539. spin_lock_irqsave(&priv->lock, flags);
  540. priv->throttled = 0;
  541. priv->actually_throttled = 0;
  542. spin_unlock_irqrestore(&priv->lock, flags);
  543. port->read_urb->dev = port->serial->dev;
  544. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  545. if (result)
  546. dev_err(&port->dev,
  547. "%s - failed submitting read urb, error %d\n",
  548. __func__, result);
  549. }
  550. static int palm_os_3_probe(struct usb_serial *serial,
  551. const struct usb_device_id *id)
  552. {
  553. struct device *dev = &serial->dev->dev;
  554. struct visor_connection_info *connection_info;
  555. unsigned char *transfer_buffer;
  556. char *string;
  557. int retval = 0;
  558. int i;
  559. int num_ports = 0;
  560. dbg("%s", __func__);
  561. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  562. if (!transfer_buffer) {
  563. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  564. sizeof(*connection_info));
  565. return -ENOMEM;
  566. }
  567. /* send a get connection info request */
  568. retval = usb_control_msg(serial->dev,
  569. usb_rcvctrlpipe(serial->dev, 0),
  570. VISOR_GET_CONNECTION_INFORMATION,
  571. 0xc2, 0x0000, 0x0000, transfer_buffer,
  572. sizeof(*connection_info), 300);
  573. if (retval < 0) {
  574. dev_err(dev, "%s - error %d getting connection information\n",
  575. __func__, retval);
  576. goto exit;
  577. }
  578. if (retval == sizeof(*connection_info)) {
  579. connection_info = (struct visor_connection_info *)
  580. transfer_buffer;
  581. num_ports = le16_to_cpu(connection_info->num_ports);
  582. for (i = 0; i < num_ports; ++i) {
  583. switch (
  584. connection_info->connections[i].port_function_id) {
  585. case VISOR_FUNCTION_GENERIC:
  586. string = "Generic";
  587. break;
  588. case VISOR_FUNCTION_DEBUGGER:
  589. string = "Debugger";
  590. break;
  591. case VISOR_FUNCTION_HOTSYNC:
  592. string = "HotSync";
  593. break;
  594. case VISOR_FUNCTION_CONSOLE:
  595. string = "Console";
  596. break;
  597. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  598. string = "Remote File System";
  599. break;
  600. default:
  601. string = "unknown";
  602. break;
  603. }
  604. dev_info(dev, "%s: port %d, is for %s use\n",
  605. serial->type->description,
  606. connection_info->connections[i].port, string);
  607. }
  608. }
  609. /*
  610. * Handle devices that report invalid stuff here.
  611. */
  612. if (num_ports == 0 || num_ports > 2) {
  613. dev_warn(dev, "%s: No valid connect info available\n",
  614. serial->type->description);
  615. num_ports = 2;
  616. }
  617. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  618. num_ports);
  619. /*
  620. * save off our num_ports info so that we can use it in the
  621. * calc_num_ports callback
  622. */
  623. usb_set_serial_data(serial, (void *)(long)num_ports);
  624. /* ask for the number of bytes available, but ignore the
  625. response as it is broken */
  626. retval = usb_control_msg(serial->dev,
  627. usb_rcvctrlpipe(serial->dev, 0),
  628. VISOR_REQUEST_BYTES_AVAILABLE,
  629. 0xc2, 0x0000, 0x0005, transfer_buffer,
  630. 0x02, 300);
  631. if (retval < 0)
  632. dev_err(dev, "%s - error %d getting bytes available request\n",
  633. __func__, retval);
  634. retval = 0;
  635. exit:
  636. kfree(transfer_buffer);
  637. return retval;
  638. }
  639. static int palm_os_4_probe(struct usb_serial *serial,
  640. const struct usb_device_id *id)
  641. {
  642. struct device *dev = &serial->dev->dev;
  643. struct palm_ext_connection_info *connection_info;
  644. unsigned char *transfer_buffer;
  645. int retval;
  646. dbg("%s", __func__);
  647. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  648. if (!transfer_buffer) {
  649. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  650. sizeof(*connection_info));
  651. return -ENOMEM;
  652. }
  653. retval = usb_control_msg(serial->dev,
  654. usb_rcvctrlpipe(serial->dev, 0),
  655. PALM_GET_EXT_CONNECTION_INFORMATION,
  656. 0xc2, 0x0000, 0x0000, transfer_buffer,
  657. sizeof(*connection_info), 300);
  658. if (retval < 0)
  659. dev_err(dev, "%s - error %d getting connection info\n",
  660. __func__, retval);
  661. else
  662. usb_serial_debug_data(debug, &serial->dev->dev, __func__,
  663. retval, transfer_buffer);
  664. kfree(transfer_buffer);
  665. return 0;
  666. }
  667. static int visor_probe(struct usb_serial *serial,
  668. const struct usb_device_id *id)
  669. {
  670. int retval = 0;
  671. int (*startup)(struct usb_serial *serial,
  672. const struct usb_device_id *id);
  673. dbg("%s", __func__);
  674. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  675. err("active config #%d != 1 ??",
  676. serial->dev->actconfig->desc.bConfigurationValue);
  677. return -ENODEV;
  678. }
  679. if (id->driver_info) {
  680. startup = (void *)id->driver_info;
  681. retval = startup(serial, id);
  682. }
  683. return retval;
  684. }
  685. static int visor_calc_num_ports(struct usb_serial *serial)
  686. {
  687. int num_ports = (int)(long)(usb_get_serial_data(serial));
  688. if (num_ports)
  689. usb_set_serial_data(serial, NULL);
  690. return num_ports;
  691. }
  692. static int generic_startup(struct usb_serial *serial)
  693. {
  694. struct usb_serial_port **ports = serial->port;
  695. struct visor_private *priv;
  696. int i;
  697. for (i = 0; i < serial->num_ports; ++i) {
  698. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  699. if (!priv) {
  700. while (i-- != 0) {
  701. priv = usb_get_serial_port_data(ports[i]);
  702. usb_set_serial_port_data(ports[i], NULL);
  703. kfree(priv);
  704. }
  705. return -ENOMEM;
  706. }
  707. spin_lock_init(&priv->lock);
  708. usb_set_serial_port_data(ports[i], priv);
  709. }
  710. return 0;
  711. }
  712. static int clie_3_5_startup(struct usb_serial *serial)
  713. {
  714. struct device *dev = &serial->dev->dev;
  715. int result;
  716. u8 data;
  717. dbg("%s", __func__);
  718. /*
  719. * Note that PEG-300 series devices expect the following two calls.
  720. */
  721. /* get the config number */
  722. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  723. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  724. 0, 0, &data, 1, 3000);
  725. if (result < 0) {
  726. dev_err(dev, "%s: get config number failed: %d\n",
  727. __func__, result);
  728. return result;
  729. }
  730. if (result != 1) {
  731. dev_err(dev, "%s: get config number bad return length: %d\n",
  732. __func__, result);
  733. return -EIO;
  734. }
  735. /* get the interface number */
  736. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  737. USB_REQ_GET_INTERFACE,
  738. USB_DIR_IN | USB_RECIP_INTERFACE,
  739. 0, 0, &data, 1, 3000);
  740. if (result < 0) {
  741. dev_err(dev, "%s: get interface number failed: %d\n",
  742. __func__, result);
  743. return result;
  744. }
  745. if (result != 1) {
  746. dev_err(dev,
  747. "%s: get interface number bad return length: %d\n",
  748. __func__, result);
  749. return -EIO;
  750. }
  751. return generic_startup(serial);
  752. }
  753. static int treo_attach(struct usb_serial *serial)
  754. {
  755. struct usb_serial_port *swap_port;
  756. /* Only do this endpoint hack for the Handspring devices with
  757. * interrupt in endpoints, which for now are the Treo devices. */
  758. if (!((le16_to_cpu(serial->dev->descriptor.idVendor)
  759. == HANDSPRING_VENDOR_ID) ||
  760. (le16_to_cpu(serial->dev->descriptor.idVendor)
  761. == KYOCERA_VENDOR_ID)) ||
  762. (serial->num_interrupt_in == 0))
  763. goto generic_startup;
  764. dbg("%s", __func__);
  765. /*
  766. * It appears that Treos and Kyoceras want to use the
  767. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  768. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  769. * Note that swapping the bulk out endpoints would break lots of
  770. * apps that want to communicate on the second port.
  771. */
  772. #define COPY_PORT(dest, src) \
  773. do { \
  774. dest->read_urb = src->read_urb; \
  775. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;\
  776. dest->bulk_in_buffer = src->bulk_in_buffer; \
  777. dest->interrupt_in_urb = src->interrupt_in_urb; \
  778. dest->interrupt_in_endpointAddress = \
  779. src->interrupt_in_endpointAddress;\
  780. dest->interrupt_in_buffer = src->interrupt_in_buffer; \
  781. } while (0);
  782. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  783. if (!swap_port)
  784. return -ENOMEM;
  785. COPY_PORT(swap_port, serial->port[0]);
  786. COPY_PORT(serial->port[0], serial->port[1]);
  787. COPY_PORT(serial->port[1], swap_port);
  788. kfree(swap_port);
  789. generic_startup:
  790. return generic_startup(serial);
  791. }
  792. static int clie_5_attach(struct usb_serial *serial)
  793. {
  794. dbg("%s", __func__);
  795. /* TH55 registers 2 ports.
  796. Communication in from the UX50/TH55 uses bulk_in_endpointAddress
  797. from port 0. Communication out to the UX50/TH55 uses
  798. bulk_out_endpointAddress from port 1
  799. Lets do a quick and dirty mapping
  800. */
  801. /* some sanity check */
  802. if (serial->num_ports < 2)
  803. return -1;
  804. /* port 0 now uses the modified endpoint Address */
  805. serial->port[0]->bulk_out_endpointAddress =
  806. serial->port[1]->bulk_out_endpointAddress;
  807. return generic_startup(serial);
  808. }
  809. static void visor_shutdown(struct usb_serial *serial)
  810. {
  811. struct visor_private *priv;
  812. int i;
  813. dbg("%s", __func__);
  814. for (i = 0; i < serial->num_ports; i++) {
  815. priv = usb_get_serial_port_data(serial->port[i]);
  816. if (priv) {
  817. usb_set_serial_port_data(serial->port[i], NULL);
  818. kfree(priv);
  819. }
  820. }
  821. }
  822. static int __init visor_init(void)
  823. {
  824. int i, retval;
  825. /* Only if parameters were passed to us */
  826. if (vendor > 0 && product > 0) {
  827. struct usb_device_id usb_dev_temp[] = {
  828. {
  829. USB_DEVICE(vendor, product),
  830. .driver_info =
  831. (kernel_ulong_t) &palm_os_4_probe
  832. }
  833. };
  834. /* Find the last entry in id_table */
  835. for (i = 0;; i++) {
  836. if (id_table[i].idVendor == 0) {
  837. id_table[i] = usb_dev_temp[0];
  838. break;
  839. }
  840. }
  841. /* Find the last entry in id_table_combined */
  842. for (i = 0;; i++) {
  843. if (id_table_combined[i].idVendor == 0) {
  844. id_table_combined[i] = usb_dev_temp[0];
  845. break;
  846. }
  847. }
  848. info(
  849. "Untested USB device specified at time of module insertion");
  850. info("Warning: This is not guaranteed to work");
  851. info("Using a newer kernel is preferred to this method");
  852. info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
  853. vendor, product);
  854. }
  855. retval = usb_serial_register(&handspring_device);
  856. if (retval)
  857. goto failed_handspring_register;
  858. retval = usb_serial_register(&clie_3_5_device);
  859. if (retval)
  860. goto failed_clie_3_5_register;
  861. retval = usb_serial_register(&clie_5_device);
  862. if (retval)
  863. goto failed_clie_5_register;
  864. retval = usb_register(&visor_driver);
  865. if (retval)
  866. goto failed_usb_register;
  867. info(DRIVER_DESC);
  868. return 0;
  869. failed_usb_register:
  870. usb_serial_deregister(&clie_5_device);
  871. failed_clie_5_register:
  872. usb_serial_deregister(&clie_3_5_device);
  873. failed_clie_3_5_register:
  874. usb_serial_deregister(&handspring_device);
  875. failed_handspring_register:
  876. return retval;
  877. }
  878. static void __exit visor_exit (void)
  879. {
  880. usb_deregister(&visor_driver);
  881. usb_serial_deregister(&handspring_device);
  882. usb_serial_deregister(&clie_3_5_device);
  883. usb_serial_deregister(&clie_5_device);
  884. }
  885. module_init(visor_init);
  886. module_exit(visor_exit);
  887. MODULE_AUTHOR(DRIVER_AUTHOR);
  888. MODULE_DESCRIPTION(DRIVER_DESC);
  889. MODULE_LICENSE("GPL");
  890. module_param(debug, bool, S_IRUGO | S_IWUSR);
  891. MODULE_PARM_DESC(debug, "Debug enabled or not");
  892. module_param(stats, bool, S_IRUGO | S_IWUSR);
  893. MODULE_PARM_DESC(stats, "Enables statistics or not");
  894. module_param(vendor, ushort, 0);
  895. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  896. module_param(product, ushort, 0);
  897. MODULE_PARM_DESC(product, "User specified product ID");